For a long time, I thought why use a different node package manager like yarn – “npm (the package manager that’s installed when you install node) does the job, why make it more complicated?” Then I came across a project that said it installed best with pnpm, I decided to check it out and found it does add value. I’ve switched to using pnpm because it allow management of node which was previously a bit of a faff. Also, when one includes a package in a project it stores the package version in a central repo and adds a link to that package/version so next time you reinstall or use that package / version in another project it does not need to be downloaded again. It also takes care of global installations without using sudo or extra configuration. Check out the website for more benefits.
What follows is a very basic introduction to pnpm on Linux systems partly as an aid memoir to myself but hopefully you find it helpful too! These instructions should work for most Linux distros but I’ve only tried it out on Debian based ones so far.
Installing if you don’t already have node installed
If you don’t have node installed you can install pnpm and use that to install node rather adding the node repo to your system page manger and installing from there. To install so that you can manage node use the command:
curl -fsSL https://get.pnpm.io/install.sh > pnpm-install.sh
# view the file to make sure you're happy running it on your computer
or if you don’t have curl you can use “wget -O- https://get.pnpm.io/install.sh | sh -” instead.
You can install the current LTS version of node using the command (other options in place of “lts” allow installing other versions, see the pnpm website for details):
pnpm env use --global lts
Installing if you already have node installed
You can use the script above if you’ve already got node installed however there are also a couple of other If you have a recent version node (versions 16.13 onwards) you can use corepack, run the commands:
corepack prepare pnpm@latest –activate
Or you can install using npm:
npm install -g pnpm
Command are often similar to their equivalent npm command and should be run from the project directory.
I find tab completion very useful, to install it for bash zsh or fish shells use the following (and then reload your shell using e.g. source ~/.bashrc or log out and back in again):
To start a new project:
To convert an existing project using a package-lock.json:
To install an existing project – this read from an existing pnpn-lock.yaml to get version information (if it exists, it it does not exist it will be created – you should add this file to your :
Adding dependencies to a project is slightly different to npm. To add a development only dependency use the add command and append “–save-dev” as below, you can optionally specify a tag, version or version range:
pnpm add <package name>[@<tag>|@version>|@<version range>] –save-dev
Add a production dependency (the default add action)
pnpm add <package name>[@<tag>|@version>|@<version range>]
Removing the installed packages is similar to npm (though as pnpm creates links to a central repository it only removed the links to these packages)
pnpm remove <package name>
To run a script you can use the same syntax as npm (pnpm run <script defined in package.json>) but unless your script is a keyword used by pnpm you can simply use:
pnpm <script defined in package.json>
Thanks for reading
I find pnpm much is an improved package manager compared to npm, hopefully you’ve found this useful as an introduction.