Why you should use PNPM

Matthew Labrum

A comparison of Node.js version management tools and how PNPM can simplify your workflow

hero background

Node.js is a popular and versatile platform for building web applications, APIs, command-line tools, and more. However, different projects may require different versions of Node.js due to outdated or fixed dependencies and switching between them can be a hassle. In this blog post, we will explore some of the existing solutions for Node.js version management, and introduce PNPM, a fast and efficient package manager that can also handle Node.js versions.

Existing solutions for Node.js version management

NVM

https://github.com/nvm-sh/nvm

One of the most widely used tools for Node.js version management is nvm, or Node Version Manager. nvm allows you to install multiple versions of Node.js on your system, and switch between them using a simple command.

$ nvm use 20

nvm also lets you specify the Node.js version for each project using a .nvmrc file in the project root and automatically use this on the current running shell by running “nvm use”

.nvmrc
20.11.11

However, nvm has a number of drawbacks. First, it is only available for Linux and macOS, so Windows users are out of luck. Secondly, nvm does not install Node.js globally but rather in a hidden directory under your home folder. This means you have to run nvm use before running any Node.js command, or add it to your shell profile using path. Thirdly, nvm downloads and installs the entire Node.js binary for each version and will break globally installed node modules.

note:
NVM-windows (https://github.com/coreybutler/nvm-windows ) does exist, but its actually a different project implementation and does not support the same options as nvm, such as “nvm use” and the .nvmrc file for per project Node.js version locking.

NVS

NVS, or Node Version Switcher ( https://github.com/jasongin/nvs ), is a cross platform tool that allows you to switch between different versions of Node.js on your machine.

NVS lets you manage multiple Node.js installations without affecting the global environment or requiring administrative privileges.

NVS also supports per-project Node.js version locking, by reading the .node-version file in your project directory. With NVS, you can quickly switch to the Node.js version that suits your needs. Note this file is not compatible with nvm.

The downsides to NVS are similar to nvm where it installs the desired Node.js version into a hidden directory then maps that before the existing Node.js install by editing the currently running path requiring all developers to run the NVS command within their IDE before executing any node commands.

Introducing PNPM

PNPM, or Performant Node Package Manager, is a fast and efficient alternative to npm, the default package manager for Node.js. PNPM uses a novel approach to package installation, which avoids duplication of files and saves disk space. PNPM also has some features that make it stand out from other package managers, such as:

  • Twice as fast as NPM
  • Support for overriding dependencies or patching packages
  • Support for monorepos, or projects that contain multiple packages in a single repository
  • Support for hooks, or custom scripts that run before or after certain commands
  • Easily reduce a development node_modules down and “publish” only the needed packages in a docker container
  • Support for installing multiple OS/Platform dependencies for a NPM package, allowing you to share the same node_modules folder between your operating system and your docker container during development.

In another blog post I'll cross compare NPM and PNPM.

But what does PNPM have to do with Node.js version management? Well, PNPM has a built-in feature that can automatically download and use the Node.js version that matches your project requirements.

This feature is called use-node-version (https://pnpm.io/npmrc#use-node-version), and it works by reading the Node.js version from the .npmrc file in your project root.

How to use PNPM for per project Node.js version management

There are a few ways to install and start using PNPM, such as the new “Corepack” feature of npm which will automatically install PNPM when used, which I’ll detail in another blog post, but for the moment we’ll run pnpm as a global installation.

For the most up to date way to install PNPM see https://pnpm.io/installation

To use PNPM for Node.js version management, you need to do the following steps:

  1. Install PNPM globally on your system using npm install -g pnpm (This assumes you already have node.js installed, see the next section for how to install PNPM without node.js installed)
  2. Create a .npmrc file in your project root, and add the field use-node-version with the Node.js version you want to use. For example, use-node-version=20.11.1
  3. Run PNPM commands as usual, such as pnpm install or pnpm run. PNPM will automatically download and use the Node.js version specified in the .npmrc file, if it is not already installed on your system.

An example console showing pnpm automatically fetching a specific Node.js version before executing the task

$ pnpm run dev
 *  Executing task: pnpm run dev:next 

Fetching Node.js 20.11.1 ...

> lynkzcomau@1.0.0 dev:next
> next dev

   ▲ Next.js 14.1.0
   - Local:        http://localhost:3000
   - Environments: .env.development

 ✓ Ready in 2s

That's it! You can now use PNPM for Node.js version management and enjoy the benefits of a fast and efficient package manager. You can also use PNPM for other projects that do not require a specific Node.js version, as PNPM will use the default Node.js version on your system.

As PNPM manages which Node.js version it calls under the hood, each developer can use any IDE/terminal/OS they want without having to configure additional settings to override paths or call shell scripts before executing npm commands.

If you want to learn more about PNPM, you can visit its official website at https://pnpm.io/, or check out its GitHub repository at https://github.com/pnpm/pnpm.

How to use PNPM for global Node.js version management

If you already have Node.js installed through nvm/NVS/another tool or standalone its recommended to uninstall Node.js first.

Once you’ve uninstalled Node.js you can install PNPM with the following commands (also see https://pnpm.io/installation for the most up to date methods)

On Windows using PowerShell:

iwr https://get.pnpm.io/install.ps1 -useb | iex

On POSIX systems

$ curl -fsSL https://get.pnpm.io/install.sh | sh -

Note: This install will not install corepack, if you wish to add corepack to Node.js run the following command (I’ll cover exactly what corepack is and how to use it, in another blogpost)

$ pnpm add -g corepack

Once you’ve installed PNPM run the following commands on the command line to set a global version of Node.js

$ pnpm -g env use 20

Done! This will install a global version of Node.js version 20, you can also specify the version explicitly e.g 20.11.1

If you want to list and see the installed Node.js versions you can use the following command

$ pnpm -g env list

For more information on globally managing Node.js with PNPM see https://pnpm.io/cli/env