Running Multiple Versions of Node.js with Node Version Manager

joshtronic

If you work on multiple Node.js projects, you’ve probably run into this one time or another. You have the latest and greatest version of Node.js installed, and the project you’re about to work on requires an older version. In those situations, the Node Version Manager (nvm) has your back, allowing you to install multiple versions of Node.js and switch between them as you see fit.

Getting started

To get started, you will need to install the Node Version Manager, or nvm on your system. It is included by many package managers, or you can install it manually by running one of the following:

# If you have `curl` installed:
$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash

# Or if you prefer `wget`:
$ wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash

Once installed, you will also need to add a couple of lines to your shell’s startup file (.bashrc, .bash_profile, .zshrc, et cetera):

export NVM_DIR="${XDG_CONFIG_HOME/:-$HOME/.}nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

Don’t forget to reload your configuration by sourceing the file, or simply closing and reopening your favorite terminal application.

You can also opt to paste those lines directly into your terminal each time you’d like to use nvm.

Installing Multiple Node.js Versions

Now that we have everything installed, let’s install a few different versions of Node.js:

$ nvm install 0.10
Downloading and installing node v0.10.48...
Downloading https://nodejs.org/dist/v0.10.48/node-v0.10.48-linux-x64.tar.xz...
########################################################################################
100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v0.10.48 (npm v2.15.1)

$ nvm install 8
Downloading and installing node v8.16.0...
Downloading https://nodejs.org/dist/v8.16.0/node-v8.16.0-linux-x64.tar.xz...
########################################################################################
100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v8.16.0 (npm v6.4.1)

$ nvm install 12
Downloading and installing node v12.7.0...
Downloading https://nodejs.org/dist/v12.7.0/node-v12.7.0-linux-x64.tar.xz...
########################################################################################
100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v12.7.0 (npm v6.10.0)

Upon running each command, nvm will download the version of Node.js from the official website and install it. Once installed, it will also set the version you just installed as the active version.

If you were to run node --version after each of the aforementioned commands, you’d see the most recent version of the respective major version.

nvm isn’t limited to major versions either, you could also run nvm install 12.0.0 to explicitly install the 12 dot oh dot oh version of Node.js.

Listing Installed Node.js Versions

With a handful of different versions of Node.js installed, we can run nvm with the ls argument to list out everything we have installed:

$ nvm ls
       v0.10.48
         v4.9.1
        v6.10.3
        v6.14.4
         v8.4.0
        v8.10.0
       v10.13.0
       v10.15.0
       v10.15.3
->      v12.0.0
        v12.7.0
         system
default -> v10.15 (-> v10.15.3)
node -> stable (-> v12.7.0) (default)
stable -> 12.7 (-> v12.7.0) (default)
iojs -> N/A (default)
unstable -> N/A (default)
lts/* -> lts/dubnium (-> N/A)
lts/argon -> v4.9.1
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.16.0 (-> N/A)
lts/dubnium -> v10.16.0 (-> N/A)

Yes, as a matter of fact, I do have a bunch of different versions installed. Obviously, your output will probably look a little different than my own.

The little -> indicates the active version, and default -> indicates the default version of Node.js, the one that will be available when you open a new shell.

system corresponds with the version of Node.js installed outside of nvm on your system.

Setting a Default Node.js Version

Even with juggling multiple versions, there’s a good chance you have one version that you would prefer to run the majority of the time.

Often times, that would be the latest stable version of Node.js, leveraging nvm for being able to tap older versions.

To set the latest stable version as your default, run:

$ nvm alias default stable
default -> stable (-> v12.7.0)

If you’re like me, you have a specific version number you would like to set as your default. To alias default to a specific version, run:

$ nvm alias default 10.15
default -> 10.15 (-> v10.15.3)

Now every time you open a new shell, that version of Node.js will be immediately available.

Switching Between Node.js Versions

What good are all these versions of Node.js if we can’t switch between them?

To switch to a different version of Node.js, simply tell nvm which version you’d like to use:

$ nvm use 0.10
Now using node v0.10.48 (npm v2.15.1)

$ nvm use 8
Now using node v8.16.0 (npm v6.4.1)

$ nvm use 12
Now using node v12.7.0 (npm v6.10.0)

You can even switch back to your default version:

$ nvm use default
Now using node v10.15.3 (npm v6.9.0)

Removing Node.js Versions

As you saw earlier, I have a bunch of different versions of Node.js installed due to working on a variety of projects over the past year on this particular laptop.

Between you and me, I don’t even remember which projects I had installed some of those versions for.

Fortunately, we can remove Node.js versions just as easily as we installed them:

$ nvm uninstall 0.10
Uninstalled node v0.10.48

$ nvm uninstall 4
Uninstalled node v4.9.1

Unfortunately, when you specify a major or minor version, nvm will only uninstall the latest installed version that matches the version number.

Because I have 2 different version of Node.js version 6 installed, I have to run the uninstall command for each version:

$ nvm uninstall 6
Uninstalled node v6.14.4

$ nvm uninstall 6
Uninstalled node v6.10.3

Worth noting, you can’t remove a version of Node.js that is currently in use and active.

Unloading Node Version Manager

Had enough and want to get back to your system’s installed version of Node.js?

You could simply switch the system version the same way we switched to other versions by running:

$ nvm use system
Now using system version of node: v11.15.0 (npm v6.10.1)

Yes, my system is a bit behind right now.

Or if you’d prefer to unload nvm entirely for your session, you can simply run nvm unload.

Conclusion

Working on multiple projects that utilize different versions of Node.js doesn’t have to be a nightmare.

Node Version Manager makes the process quite painless and even takes the headache out of remembering to switch versions by supporting automatic switching by way of an .nvmrc file in your project’s root:

$ echo "12" > .nvmrc

And you’re on your way!

  Tweet It

🕵 Search Results

🔎 Searching...

Sponsored by #native_company# — Learn More
#native_title# #native_desc#
#native_cta#