Download source from npm without installing it

2019-01-17 02:21发布

问题:

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

回答1:

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



回答2:

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



回答3:

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

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


回答4:

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

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

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

"v" is just another alias for view:

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



回答5:

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>/.

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



回答6:

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

#!/bin/bash

if [[ $# -eq 0 ]] ; then
    echo "Usage: $0 jquery bootstrap@3 tinymce@4.5"
    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


回答7:

Why don't you create an empty directory outside your project, do an npm install there, and get the source from node_modules.

 cd /tmp
 mkdir dir1
 cd dir1
 npm install intersting_module
 cd node_modules

the full module is right there.

You could also go on http://npmjs.org, look for the module there. Most modules will list there repository and you can get the code from there.