What is pnpm?
Introduction to pnpm, the drop in npm replacement that saves you disk space
I recently wrote about how we have huge node_modules folders and why this is not necessarily a bad thing, but it would be reduce that hard drive consumption, right?
Every byte saved on disk can be used for something else than libraries code, I have a 512GB SSD on my MacBook Pro I bought in 2010 but some brand new computers in 2019 ship with a 128GB SSD (something went wrong with Mooreβs Law when it comes to hard disk space).
In particular, one way would be to centralize the libraries code storage into a central place, and share it with all the projects you work on.
This is the main value proposition of pnpm, a very cool project you can check out at https://pnpm.js.org.
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 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.
You install pnpm using npm, of course π
npm install -g pnpm
Then being pnpm a drop-in replacement, you can use all the 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.
For example Glitch is one of those companies, as they host a gazillion Node.js projects.
pnpm gives them, in addition to the npm usual commands, some utilities including pnpm recursive, which is used to run the same command across all the projects in a folder. For example you can initialize 100 projects stored in the current folder by running pnpm recursive install. Handy.
If you use npx, which is a handy (and the recommended) way to run utilities like create-react-app, youβll get the benefits of pnpm by using the pnpx command which comes with pnpm:
pnpx create-react-app my-cool-new-app
Where are the packages installed? In macOS, in the ~/.pnpm-store/ folder (where ~ means your home folder). I installed lodash as an example and this was the resulting folder structure:
β ~ 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
There are many more advanced things to learn about the tool, but I hope this helps getting you started with pnpm!
Should you use it for a day-to-day use? Probably not, just stick to npm unless you have needs that this tool solves for you - lack of disk space being one of them.
download all my books for free
- javascript handbook
- typescript handbook
- css handbook
- node.js handbook
- astro handbook
- html handbook
- next.js pages router handbook
- alpine.js handbook
- htmx handbook
- react handbook
- sql handbook
- git cheat sheet
- laravel handbook
- express handbook
- swift handbook
- go handbook
- php handbook
- python handbook
- cli handbook
- c handbook
subscribe to my newsletter to get them
Terms: by subscribing to the newsletter you agree the following terms and conditions and privacy policy. The aim of the newsletter is to keep you up to date about new tutorials, new book releases or courses organized by Flavio. If you wish to unsubscribe from the newsletter, you can click the unsubscribe link that's present at the bottom of each email, anytime. I will not communicate/spread/publish or otherwise give away your address. Your email address is the only personal information collected, and it's only collected for the primary purpose of keeping you informed through the newsletter. It's stored in a secure server based in the EU. You can contact Flavio by emailing flavio@flaviocopes.com. These terms and conditions are governed by the laws in force in Italy and you unconditionally submit to the jurisdiction of the courts of Italy.