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.
npm, of course 😁
npm install -g pnpm
pnpm a drop-in replacement, you can use all the
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
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
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 my free Node.js Handbook
More node tutorials:
- An introduction to the npm package manager
- Introduction to Node.js
- HTTP requests using Axios
- Where to host a Node.js app
- Interact with the Google Analytics API using Node.js
- The npx Node Package Runner
- The package.json guide
- Where does npm install the packages?
- How to update Node.js
- How to use or execute a package installed using npm
- The package-lock.json file
- Semantic Versioning using npm
- Should you commit the node_modules folder to Git?
- Update all the Node dependencies to their latest version
- Parsing JSON with Node.js
- Find the installed version of an npm package
- Node.js Streams
- Install an older version of an npm package
- Get the current folder in Node
- How to log an object in Node
- Expose functionality from a Node file using exports
- Differences between Node and the Browser
- Make an HTTP POST request using Node
- Get HTTP request body data using Node
- Node Buffers
- A brief history of Node.js
- How to install Node.js
- How to use the Node.js REPL
- Node, accept arguments from the command line
- Output to the command line using Node
- Accept input from the command line in Node
- Uninstalling npm packages with `npm uninstall`
- npm global or local packages
- npm dependencies and devDependencies
- The Node.js Event Loop
- Understanding process.nextTick()
- Understanding setImmediate()
- The Node Event emitter
- Build an HTTP Server
- Making HTTP requests with Node
- The Node fs module
- HTTP requests in Node using Axios
- Reading files with Node
- Node File Paths
- Writing files with Node
- Node file stats
- Working with file descriptors in Node
- Working with folders in Node
- The Node path module
- The Node http module
- Using WebSockets with Node.js
- The basics of working with MySQL and Node
- Error handling in Node.js
- The Pug Guide
- How to read environment variables from Node.js
- How to exit from a Node.js program
- The Node os module
- The Node events module
- Node, the difference between development and production
- How to check if a file exists in Node.js
- How to create an empty file in Node.js
- How to remove a file with Node.js
- How to get the last updated date of a file using Node.js
- How to write a JSON object to file in Node.js
- Why should you use Node.js in your next project?
- Run a web server from any folder
- How to use MongoDB with Node.js
- Use the Chrome DevTools to debug a Node.js app
- What is pnpm?
- The Node.js Runtime v8 options list
- How to fix the "Missing write access" error when using npm
- How to enable ES Modules in Node.js
- How to spawn a child process with Node.js
- How to get both parsed body and raw body in Express
- How to handle file uploads in Node.js
- What are peer dependencies in a Node module?
- How to write a CSV file with Node.js
- How to read a CSV file with Node.js
- The Node Core Modules
- Incrementing multiple folders numbers at once using Node.js
- How to print a canvas to a data URL
- How to create and save an image with Node.js and Canvas
- How to download an image using Node.js
- How to mass rename files in Node.js
- How to get the names of all the files in a folder in Node
- How to use promises and await with Node.js callback-based functions
- How to test an npm package locally
- How to check the current Node.js version at runtime
- How to use Sequelize to interact with PostgreSQL
- Serve an HTML page using Node.js
- How to solve the `util.pump is not a function` error in Node.js