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,
npm
readspackage.json
file and starts checking the defined version onnpm registry
- On matching the version, it firsts download the package in
global cache directory
of the device - Later copies the package on the
node_modules
directory of theproject specific directory
- While running the project, packages in the
project specific directory
is used - The
global cache directory
helpsnpm
to find the specific version ofpackage
when 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 - I am 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_modules
directories 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 install
requires a copy of the package twice. First inglobal cache directory
and 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.
-
pnpm
download packages fromnpm registry
similar tonpm
- It stores packages in
global cache directory
same asnpm
- Now the magic happens. Instead of copying the packages to
project specific directory
one by one according topackage.json
file, it hard links the package tonode_modules
ofproject specific directory
. - It seems copying, but it is not. As it just links (similar to file/folder shortcut) the package path from
global cache directory
toproject specific directory
without 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
pnpm
globally 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 installpnpm
globally -
Ensure your pnpm executable path is in your global
bash PATH
. You can add it in your.bashrc
or.zshrc
file in following way:
export PNPM_HOME="/Users/ankur/Library/pnpm" export PATH="$PNPM_HOME:$PATH"
pnpm
has 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 the
scripts
section ofpackage.json
filter the workspaces
"w:server": "pnpm --filter server", "w:client": "pnpm --filter client",
Then u can easily run workspace specific command. Something like this
pnpm w:server build
You can easily use
pnpm
in dockerpnpm
can 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!
I hope you've found the article useful. U should try pnpm
if u haven't already. Feel free to share your thoughts in the comments below.
Check more on
Top comments (0)