How to download npm package without installing it
Sometimes I need to get an npm package content in an archive without installing it in a project. I can always install it in some test project and just copy the necessary files, but is there a better way to do it? Let's find out.
Using npm pack
The easiest way to download a package is to use npm pack
command. It creates a tarball of a requested package. As an example let's try to download a react
package. First, I will add a --dry-run
flag to see the content without downloading:
npm pack --dry-run react
❯ npm pack --dry-run react
npm notice
npm notice 📦 [email protected]
npm notice Tarball Contents
npm notice 1.1kB LICENSE
npm notice 1.2kB README.md
npm notice 42.1kB cjs/react-jsx-dev-runtime.development.js
npm notice 343B cjs/react-jsx-dev-runtime.production.min.js
npm notice 342B cjs/react-jsx-dev-runtime.profiling.min.js
npm notice 42.7kB cjs/react-jsx-runtime.development.js
npm notice 859B cjs/react-jsx-runtime.production.min.js
npm notice 858B cjs/react-jsx-runtime.profiling.min.js
npm notice 87.6kB cjs/react.development.js
npm notice 6.9kB cjs/react.production.min.js
npm notice 501B cjs/react.shared-subset.development.js
npm notice 351B cjs/react.shared-subset.production.min.js
npm notice 190B index.js
npm notice 222B jsx-dev-runtime.js
npm notice 214B jsx-runtime.js
npm notice 999B package.json
npm notice 218B react.shared-subset.js
npm notice 109.9kB umd/react.development.js
npm notice 10.8kB umd/react.production.min.js
npm notice 10.8kB umd/react.profiling.min.js
npm notice Tarball Details
npm notice name: react
npm notice version: 18.3.1
npm notice filename: react-18.3.1.tgz
npm notice package size: 81.8 kB
npm notice unpacked size: 318.1 kB
npm notice shasum: 49ab892009c53933625bd16b2533fc754cab2891
npm notice integrity: sha512-wS+hAgJShR0Kh[...]+i7F2pTVj+2iQ==
npm notice total files: 20
npm notice
react-18.3.1.tgz
As we can see it lists all files that the react
package contains, but --dry-run
does not download anything. Let's now run it without this flag:
❯ npm pack react
npm notice
npm notice 📦 [email protected]
npm notice Tarball Contents
npm notice 1.1kB LICENSE
npm notice 1.2kB README.md
npm notice 42.1kB cjs/react-jsx-dev-runtime.development.js
npm notice 343B cjs/react-jsx-dev-runtime.production.min.js
npm notice 342B cjs/react-jsx-dev-runtime.profiling.min.js
npm notice 42.7kB cjs/react-jsx-runtime.development.js
npm notice 859B cjs/react-jsx-runtime.production.min.js
npm notice 858B cjs/react-jsx-runtime.profiling.min.js
npm notice 87.6kB cjs/react.development.js
npm notice 6.9kB cjs/react.production.min.js
npm notice 501B cjs/react.shared-subset.development.js
npm notice 351B cjs/react.shared-subset.production.min.js
npm notice 190B index.js
npm notice 222B jsx-dev-runtime.js
npm notice 214B jsx-runtime.js
npm notice 999B package.json
npm notice 218B react.shared-subset.js
npm notice 109.9kB umd/react.development.js
npm notice 10.8kB umd/react.production.min.js
npm notice 10.8kB umd/react.profiling.min.js
npm notice Tarball Details
npm notice name: react
npm notice version: 18.3.1
npm notice filename: react-18.3.1.tgz
npm notice package size: 81.8 kB
npm notice unpacked size: 318.1 kB
npm notice shasum: 49ab892009c53933625bd16b2533fc754cab2891
npm notice integrity: sha512-wS+hAgJShR0Kh[...]+i7F2pTVj+2iQ==
npm notice total files: 20
npm notice
react-18.3.1.tgz
❯ ls -al
total 160
drwxr-xr-x 3 piotrpliszko staff 96 25 lip 23:11 .
drwxr-xr-x 16 piotrpliszko staff 512 25 lip 23:09 ..
-rw-r--r-- 1 piotrpliszko staff 81751 25 lip 23:11 react-18.3.1.tgz
When I decompress the archive I can see that all files are in place:
❯ tar -xvf react-18.3.1.tgz
x package/LICENSE
x package/index.js
x package/jsx-dev-runtime.js
x package/jsx-runtime.js
x package/cjs/react-jsx-dev-runtime.development.js
x package/cjs/react-jsx-dev-runtime.production.min.js
x package/cjs/react-jsx-dev-runtime.profiling.min.js
x package/cjs/react-jsx-runtime.development.js
x package/cjs/react-jsx-runtime.production.min.js
x package/cjs/react-jsx-runtime.profiling.min.js
x package/cjs/react.development.js
x package/umd/react.development.js
x package/cjs/react.production.min.js
x package/umd/react.production.min.js
x package/umd/react.profiling.min.js
x package/cjs/react.shared-subset.development.js
x package/react.shared-subset.js
x package/cjs/react.shared-subset.production.min.js
x package/package.json
x package/README.md
❯ eza --tree
.
├── package
│ ├── cjs
│ │ ├── react-jsx-dev-runtime.development.js
│ │ ├── react-jsx-dev-runtime.production.min.js
│ │ ├── react-jsx-dev-runtime.profiling.min.js
│ │ ├── react-jsx-runtime.development.js
│ │ ├── react-jsx-runtime.production.min.js
│ │ ├── react-jsx-runtime.profiling.min.js
│ │ ├── react.development.js
│ │ ├── react.production.min.js
│ │ ├── react.shared-subset.development.js
│ │ └── react.shared-subset.production.min.js
│ ├── index.js
│ ├── jsx-dev-runtime.js
│ ├── jsx-runtime.js
│ ├── LICENSE
│ ├── package.json
│ ├── react.shared-subset.js
│ ├── README.md
│ └── umd
│ ├── react.development.js
│ ├── react.production.min.js
│ └── react.profiling.min.js
└── react-18.3.1.tgz
Alternative approach - using curl
to manually download tarball
If you use an older npm
version that does not support npm pack
, you can use npm view
to obtain a tarball URL to download. If you call npm view react
without extra parameters you will get some details on a react
package:
❯ npm view react
[email protected] | MIT | deps: 1 | versions: 1875
React is a JavaScript library for building user interfaces.
https://reactjs.org/
keywords: react
dist
.tarball: https://registry.npmjs.org/react/-/react-18.3.1.tgz
.shasum: 49ab892009c53933625bd16b2533fc754cab2891
.integrity: sha512-wS+hAgJShR0KhEvPJArfuPVN1+Hz1t0Y6n5jLrGQbkb4urgPE/0Rve+1kMB1v/oWgHgm4WIcV+i7F2pTVj+2iQ==
.unpackedSize: 318.1 kB
dependencies:
loose-envify: ^1.1.0
maintainers:
- gnoff <[email protected]>
- fb <[email protected]>
- sophiebits <[email protected]>
- react-bot <[email protected]>
dist-tags:
beta: 19.0.0-beta-26f2496093-20240514 experimental: 0.0.0-experimental-14a4699f-20240725 next: 19.0.0-rc-14a4699f-20240725
canary: 19.0.0-rc-14a4699f-20240725 latest: 18.3.1 rc: 19.0.0-rc-14a4699f-20240725
published 3 months ago by react-bot <[email protected]>
As you can see there is a tarball URL that we can download. To get the tarball URL only, you can use npm view react dist.tarball
command:
❯ npm view react dist.tarball
https://registry.npmjs.org/react/-/react-18.3.1.tgz
We can combine it with curl
to download the file:
curl -O $(npm view react dist.tarball)
❯ curl -O $(npm view react dist.tarball)
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 81751 100 81751 0 0 1254k 0 --:--:-- --:--:-- --:--:-- 1267k
❯ ls -al
total 160
drwxr-xr-x 3 piotrpliszko staff 96 25 lip 23:21 .
drwxr-xr-x 16 piotrpliszko staff 512 25 lip 23:09 ..
-rw-r--r-- 1 piotrpliszko staff 81751 25 lip 23:21 react-18.3.1.tgz
After decompressing using tar -xvf react-18.3.1.tgz
we get the same result as with the first approach.
The second approach is also useful if you don't have an npm
installed. You can find package versions and tarball URLs manually or by parsing npm registry information (for example, for react
package available under https://registry.npmjs.org/react
). Then, you just use curl
to download a package from the obtained URL without a need for npm
to be installed.