How can I download the source code of a package from npm without actually installing it (i.e. without using npm install thepackage
)?
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
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:
package
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.
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
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
You can use npm view [package name] dist.tarball
which will return the URL of the compressed package file.
Here's an example using wget
to download the tarball:
wget $(npm view lodash dist.tarball)