Skip to main content

tyler@zealousOS: ~/articles/managing-multiple-versions-of-node-with-nvm$

Managing Multiple Versions of Node.js

A Short Tutorial on Node Version Manager

Categories:

What is Node Version Manager

Node Version Manager is one of my favorite CLI tools. As stated on the project’s git repository, it is a:

POSIX-compliant bash script to manage multiple active node.js versions

NVM makes it easy to have multiple versions of Node.js installed at once on your system and provides a simple CLI interface for switching between them.

Why Use Node Version Manager

If you are working on different Node.js projects on the same machine, each project will likely have its own requirements for which version of Node.js you must use to run it.

By using NVM, you can reduce complexity related to managing these versions.

Installation

Installing NVM is super simple thanks to the provided install.sh script.

All you need to do is grab the script, run it, and source your updated dotfile (usually .bashrc).

Downloading the script

If you have curl or wget installed on your machine, you can grab the script and run it via this one-liner. Don’t forget to update the version number to your desired release.

1
2
3
4
5
# Use curl to download and run the install script for NVM v0.40.3
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash

# Or use wget instead...
# wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash
Ensuring NVM is in your $PATH

The install.sh script automatically makes changes to your dotfile(s) so that your shell can load NVM and manage Node.js versions correctly.

After you have run the script, all you need to do is open a new shell session.

If you would like to remain in the current session, you can source your updated .bashrc file.

1
2
# Make current shell session aware of the changes made by NVM
source ~/.bashrc
Verifying Installation

To verify that NVM was successfully installed on your machine, you can run:

1
2
nvm --version
# Output: 0.40.3

Installing Node.js with NVM

At this point, you are now ready to use the tool to install a version of Node.js.

To install the LTS version of Node.js, simply run:

1
nvm install --lts

Listing Installed Versions of Node.js

If you would like to check what versions of Node.js are installed on your local machine, you can use:

1
nvm ls

This will return output similar to the following:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
you@host: ~$ nvm ls
->     v22.18.0
default -> lts/* (-> v22.18.0)
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v22.18.0) (default)
stable -> 22.18 (-> v22.18.0) (default)
lts/* -> lts/jod (-> v22.18.0)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.24.1 (-> N/A)
lts/erbium -> v12.22.12 (-> N/A)
lts/fermium -> v14.21.3 (-> N/A)
lts/gallium -> v16.20.2 (-> N/A)
lts/hydrogen -> v18.20.8 (-> N/A)
lts/iron -> v20.19.4 (-> N/A)
lts/jod -> v22.18.0

In the above example, v22.18.0 is the only version I have installed. It is also the version NVM is using by default.

What’s Not Installed

In the above output from nvm ls, there are a lot of lines that may seem confusing. Let’s clarify what is going on here.

Anything that shows -> N/A is not installed locally. These are just LTS aliases that NVM could install if you ask.

Switching Between Versions of Node.js

After you have installed more than one version of Node.js on your system, you can use nvm to switch between them at any time:

1
nvm use <version/alias>

Using a .nvmrc File to Pin Node.js to a Project

In the root directory of your project, you can create a .nvmrc file containing the version of Node.js you would like to pin to the project.

A .nvmrc file is just a text file that contains a version of Node.js.

For example, this would be the contents of a .nvmrc file using a version number.

1
v20.11.1

If you would like to use an alias instead, the contents would be something like the following:

1
lts/jod

This enables you to simply switch to the pinned version by running nvm use in the directory containing the .nvmrc file.

Uninstalling a Version of Node.js

Using NVM to uninstall a local version of Node.js is simple. All you need to do is run:

1
nvm uninstall <version/alias>

Wrapping Up

NVM simplifies Node.js version management, letting you focus on building projects instead of wrestling with environment conflicts.

It is not the only tool out there for the job, but has been my go-to over the years.