It’s easy to install versions of Node.js with various package managers (Homebrew on Mac, etc.) but there are situations where version consistency is important–in a team environment, for example–and using different versions of Node can introduce subtle incompatibilities, or cause tests to fail.

In that case it can be useful to manage Node installations with a package manager. One lightweight option for this is n, which aims to have a simple API and little setup and interference in a shell environment.

How to install n

Installing n is easy, but completely removing an existing version of node can be somewhat more involved. [n may also be installed in parallel with an existing Node version, but this guide is for those who want to begin with a clean slate.]

With Homebrew for example, it may resemble the following:

$ brew uninstall node
Error: Refusing to uninstall /usr/local/Cellar/node/14.4.0
because it is required by yarn, which is currently installed.
You can override this and force removal with:
  brew uninstall --ignore-dependencies n

So Yarn needs to be uninstalled first, or ignored, which is not recommended because it will cause problems later.

$ brew uninstall yarn
$ brew uninstall node

Afterward, there may still be residual directories in several locations that need removal, depending on the operating system:

  • ~/.npm
  • ~/.node-gyp
  • /usr/local/lib (look for node and especially node_modules
  • /usr/local/share/man/man1/npm* (many files)

[Note: This is macOS/Darwin, other locations may include directories under /opt such as /opt/local, etc.]

When this is finished, it is advisable to check that no additional versions remain:

$ which node
$ which npm

Installation

The README provides several options for installation. In this case n will be installed by cloning the repo and building with make, and in a home directory to avoid using sudo:

  1. $ git clone https://github.com/tj/n
  2. PREFIX=$HOME/.node make install
  3. Add an environment variable called ‘N_PREFIX’: export N_PREFIX="$HOME/.node"
  4. If this variable is not properly set, the error Error: sudo required (or change ownership, or define N_PREFIX) may be seen when installing a Node version
  5. $ n latest will install the latest Node.js version to the directory (N_PREFIX)/n/versions. After installation, this may be verified there, or by confirming that which node and which npm point to this location.

Commands

n -h or n --help will output n’s available commands. Examples include:

  1. n ls-remote - Display the most recent 20 Node versions available
  2. n <version number> - Install a specific Node version by its number
  3. n ls and n rm <version number> - Will list all locally installed versions, or remove a specific one respectively

About Yarn

Yarn should have been uninstalled earlier as described above. NPM is quite functional on its own now, but if Yarn is still desired be careful, because installing it again with Homebrew will again install Node, which is a Yarn dependency.

For that reason it is recommended to reinstall Yarn with NPM itself:

$ npm i -g yarn

Its location–which should be (N_PREFIX)/bin/yarn–can be confirmed with which yarn.

n - Node.js version management