node.js 从 npm 下载源代码而不安装

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15035786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 13:40:33  来源:igfitidea点击:

Download source from npm without installing it

node.jspackagenpm

提问by AURIGADL

How can I download the source code of a package from npm without actually installing it (i.e. without using npm install thepackage)?

如何从 npm 下载包的源代码而不实际安装它(即不使用npm install thepackage)?

回答by Gustavo Rodrigues

You can use npm view [package name] dist.tarballwhich will return the URL of the compressed package file.

您可以使用npm view [package name] dist.tarballwhich 将返回压缩包文件的 URL。

Here's an example using wgetto download the tarball:

这是一个wget用于下载 tarball的示例:

wget $(npm view lodash dist.tarball)

回答by grahamaj

A simpler way to do this is npm pack <package_name>. This will retrieve the tarball from the registry, place it in your npm cache, and put a copy in the current working directory. See https://docs.npmjs.com/cli/pack

一种更简单的方法是npm pack <package_name>. 这将从注册表中检索 tarball,将其放入您的 npm 缓存中,并将副本放入当前工作目录中。见https://docs.npmjs.com/cli/pack

回答by Matteo T.

If you haven't installed npm, with the current public API, you can also access the information about a package in the npm registry from the URL https://registry.npmjs.org/<package-name>/.

如果您还没有安装 npm,使用当前的公共 API,您还可以从 URL 访问 npm 注册表中有关包的信息https://registry.npmjs.org/<package-name>/

Then you can navigate the JSON at versions > (version number) > dist > tarballto get the URL of the code archive and download it.

然后您可以在 JSON 中导航versions > (version number) > dist > tarball以获取代码存档的 URL 并下载它。

回答by fregante

npm pack XXXis the quickest to type and it'll download an archive.

npm pack XXX是最快的输入,它会下载一个档案。

Alternatively:

或者:

npm v XXX dist.tarball | xargs curl | tar -xz

this command will also:

此命令还将:

  • Download the package with progress bar
  • Extracts into a folder called package
  • 下载带有进度条的包
  • 解压到一个名为 package

回答by Marcs

On linux I usually download the tarball of a package like this:

在 linux 上,我通常下载这样的包的 tarball:

wget `npm v [package-name] dist.tarball`

Notice the backticks ``, on stackoverflow I cannot see them clearly.

注意反引号``,在stackoverflow上我看不清楚。

"v" is just another alias for view:

“v”只是视图的另一个别名:

https://docs.npmjs.com/cli/view

https://docs.npmjs.com/cli/view

回答by Sergey Nagaytsev

Based on Gustavo Rodrigues's answer, fixes "package" directory in .tgz, adds latest minor version discovery.

基于Gustavo Rodrigues 的回答,修复了 .tgz 中的“package”目录,添加了最新的次要版本发现。

#!/bin/bash

if [[ $# -eq 0 ]] ; then
    echo "Usage: ##代码## jquery bootstrap@3 [email protected]"
    exit 64 ## EX_USAGE
fi

set -e ## So nothing gets deleted if download fails

for pkg_name in "$@"
do

    ## Get latest version, also works with plain name
    url=$( npm v $pkg_name dist.tarball | tail -n 1 | cut -d \' -f 2 )
    tmp_dir=$( mktemp -d -p . "${pkg_name}__XXXXXXXXX" )

    ## Unpacks to directory named after package@version
    curl $url | tar -xzf - --strip 1 --directory $tmp_dir
    rm -rf $pkg_name
    mv $tmp_dir $pkg_name
done