Join the AI Workshop and learn to build real-world apps with AI. A hands-on, practical program to level up your skills.
node_modules folders can be large. Saving disk space is useful, especially on machines with limited storage.
One approach is to store packages in a single central location and share them across projects. That is what pnpm does: https://pnpm.io.
It is basically a drop-in replacement for npm, which means that once you install it, you can invoke pnpm install to download a project’s dependencies, and all will work transparently for you.
If you have 10 projects that use React, at the same version, pnpm will install it once, and then reference that first install across all your other projects.
This also means that the project initialization part takes much less time than if it had to download resources using the standard npm procedure. It’s faster even if npm cached the package, because pnpm makes a hard link to the central local repository, while npm makes a copy of the package from the cache.
Install pnpm with npm:
npm install -g pnpm
Then, since pnpm is a drop-in replacement, you can use all the usual npm commands:
pnpm install react
pnpm update react
pnpm uninstall react
and so on.
pnpm is especially appreciated in those companies where there is a need to maintain a large number of projects with the same dependencies.
pnpm recursive runs a command across all projects in a folder—for example, pnpm recursive install to install dependencies for every project in the current directory.
pnpm includes pnpx, which works like npx and uses pnpm’s store:
pnpx create-react-app my-cool-new-app
Packages are stored in ~/.pnpm-store/ (on macOS/Linux; ~ is your home directory). Example structure after installing lodash:
➜ ~ tree .pnpm-store/
.pnpm-store/
└── 2
├── _locks
├── registry.npmjs.org
│ └── lodash
│ ├── 4.17.11
│ │ ├── integrity.json
│ │ ├── node_modules
│ │ │ └── lodash
│ │ │ ├── ...
│ │ ├── package -> node_modules/lodash
│ │ └── packed.tgz
│ └── index.json
└── store.json
pnpm has more features; this covers the basics. Use it when it solves a need (e.g. saving disk space or faster installs); otherwise npm is fine for everyday use.