Download source from npm without installing it
node.jsPackageNpmnode.js Problem Overview
How can I download the source code of a package from npm without actually installing it (i.e. without using npm install thepackage
)?
node.js Solutions
Solution 1 - node.js
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)
Solution 2 - node.js
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
Solution 3 - node.js
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.
Solution 4 - node.js
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
Solution 5 - node.js
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:
Solution 6 - node.js
For simply viewing the overview of the contents of an npm package, without downloading anything locally, you can use:
npm pack --dry-run <package-name>
Demo:
$ npm pack --dry-run express
npm notice
npm notice 📦 [email protected]
npm notice === Tarball Contents ===
npm notice 110.6kB History.md
npm notice 1.2kB LICENSE
npm notice 4.8kB Readme.md
npm notice 224B index.js
npm notice 14.3kB lib/application.js
npm notice 2.4kB lib/express.js
npm notice 853B lib/middleware/init.js
npm notice 885B lib/middleware/query.js
npm notice 12.5kB lib/request.js
npm notice 27.3kB lib/response.js
npm notice 15.0kB lib/router/index.js
npm notice 3.3kB lib/router/layer.js
npm notice 4.1kB lib/router/route.js
npm notice 5.9kB lib/utils.js
npm notice 3.3kB lib/view.js
npm notice 2.8kB package.json
npm notice === Tarball Details ===
npm notice name: express
npm notice version: 4.17.2
npm notice filename: express-4.17.2.tgz
npm notice package size: 54.7 kB
npm notice unpacked size: 209.6 kB
npm notice shasum: c18369f265297319beed4e5558753cc8c1364cb3
npm notice integrity: sha512-oxlxJxcQlYwqP[...]66Ha8jCUo9QGg==
npm notice total files: 16
npm notice
express-4.17.2.tgz
Solution 7 - node.js
You can also access the content of a npm package using UNPKG
https://unpkg.com/browse/[package-name]@[package-version]/
Solution 8 - node.js
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 [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
Solution 9 - node.js
My team created OSS Gadget to make things like this easier, especially when working across different ecosystems. One of the tools in this suite is called oss-download
:
oss-download pkg:npm/express # Latest version
oss-download pkg:npm/[email protected] # Specific version
oss-download pkg:npm/express@* # All versions
oss-download -e pkg:npm/express # Decompress contents recursively
If you're only interested in npm, then npm pack
is your best option, but if you don't have npm installed or need to do similar things with PyPI, RubyGems, NuGet, etc., then OSS Gadget might be helpful.