
We will talk about pnpm, but first let’s have a brief introduction on package manager
Node.js is a popular choice for developers which allows using JavaScript on both client and server side. Having a big community, node.Js provides numerous public and private packages to integrate into projects. To manage these projects, we have package manager. npm is the standard package manager for node.js
How npm works
- On a freshly installed device,
npmreadspackage.jsonfile and starts checking the defined version onnpm registry - On matching the version, it firsts download the package in
global cache directoryof the device - Later copies the package on the
node_modulesdirectory of theproject specific directory - While running the project, packages in the
project specific directoryis used - The
global cache directoryhelpsnpmto find the specific version ofpackagewhen that version is required again
It’s kind of a good approach. However, there are some cons to this approach
- Redundant use of device space – We are pointing inefficient use of non-volatile memory (SSD, HDD). It is common that one can have multiple similar project. In that case, most of the packages (and their version) will be same. However, for each one of them, a package will be copied to multiple
project specific directory. Thosenode_modulesdirectories will gradually take up device space which u need to store other resources. U may all have seen this meme 😀


- Long installation time – U may not worry about disk space. But lengthy installation time will scare you. For a relatively simple project, required packages can be large.
npm installrequires a copy of the package twice. First inglobal cache directoryand later inproject specific directory. Copying this every time for every new project will surely kill your time
Better solution
You need to reduce your disk space and installation time. So that, u can focus on development. Now pnpm comes into the picture. The way pnpm works is simple but brilliant.
pnpmdownload packages fromnpm registrysimilar tonpm- It stores packages in
global cache directorysame asnpm - Now the magic happens. Instead of copying the packages to
project specific directoryone by one according topackage.jsonfile, it hard links the package tonode_modulesofproject specific directory. - It seems copying, but it is not. As it just links (similar to file/folder shortcut) the package path from
global cache directorytoproject specific directorywithout copying the whole file. Which saves both installation time and disk space.
Using pnpm in projects
- Install pnpm. U may install stand alone script install
pnpmglobally usingnpm - With
node & npm, It is common to usenvm (node version manager)for different version ofnode.Js. Remember that, for each version of node, u need to installpnpmglobally - Ensure your pnpm executable path is in your global
bash PATH. You can add it in your.bashrcor.zshrcfile in following way:export PNPM_HOME="/Users/ankur/Library/pnpm" export PATH="$PNPM_HOME:$PATH" pnpmhas a rich list of CLI commands for installation, update, run script. Common commands are:
| Command | Meaning |
|---|---|
pnpm add sax | Save to dependencies |
pnpm add -D sax | Save to devDependencies |
pnpm add sax@3.0.0 | Specify version 3.0.0 |
pnpm i | Install package from package.json |
pnpm rm sax | Remove package from package.json |
pnpm run <command-name> | Run command from package.json |
pnpm --filter <package_selector> <command> | Filtering allows you to restrict commands to specific subsets of packages. |
- U can also create multiple workspaces inside your project and handle all the packages from from your root directory using
pnpm. It is specially helpful for monorepo. To create workspace, just create a newpnpm-workspace.yaml. In that file you can define your workspaces. U can also useglob pattern. For example:packages: - "packages/*"Now in thescriptssection ofpackage.jsonfilter the workspaces"w:server": "pnpm --filter server", "w:client": "pnpm --filter client",Then u can easily run workspace specific command. Something like thispnpm w:server build - You can easily use
pnpmin docker pnpmcan easily be used in various continuous integration systems.
Bonus 😀
U can easily debug your server side using vscode with pnpm. U just need to add configuration in your .vscode/launch.json file
{
"name": "Launch server via pnpm",
"type": "node",
"request": "launch",
"cwd": "${workspaceFolder}",
"runtimeVersion": "18.11.0",
"runtimeExecutable": "pnpm",
"runtimeArgs": ["w:server start"]
}
End:
That’s all!
We hope you’ve found the article useful. You should try pnpm if you haven’t already. Feel free to share your thoughts in the comments below.