Precious Luke Open source crusader. I love building stuff with great technology.

How to switch Node.js versions with NVM

4 min read 1222

How To Switch Node.js Versions With NVM

When tech evolves, especially in development, versioning issues can arise. As developers, working on multiple projects simultaneously is normal, but switching between different versions of software can become painstaking and annoying.

In this article, we’ll focus on Node.js and Node Version Manager (NVM), a tool that allows users to quickly install Node versions directly from the CLI and effortlessly switch between versions.

What is Node Version Manager (NVM)?

Node Version Manager is a tool that helps us manage Node versions and is a convenient way to install Node. Think of it as npm or Yarn that helps manage Node packages, but instead of packages, NVM manages Node versions.

This also means you can install multiple Node versions onto your machine at the same time and switch among them if needed.

Why Node.js developers need NVM

Developers who work with Node often encounter this scenario: working with version 12 of Node while building a project, for example, and completing and hosting the project.

However, while everything works perfectly well, a few months later a feature may need updating, and the machine used for development runs version 14 of Node.

You won’t be surprised that running npm install or yarn install to download the Node modules in this example displays errors regarding deprecated packages.

There are also instances when you could use a higher version of Node when a project specifically requires an older version.

For instance, you might use an SPFx application or an SPFx solution that uses Node LTS v14, but you are simultaneously working on another Node application like cli-microsoft356, which works on a higher Node version.

In this case, you want to switch back and forth between different Node versions, and the easiest way to do this is using a Node version manager.

Every Node project also tends to have requirements of the version it supports. Personally, I have needed to contribute to a project that supports a Node version between 10.16 and 12 while my machine ran version 14 at the time.

Without NVM, I would not have been able to contribute to the project on the same machine without removing the existing version, which would have broken a lot, or cause dual booting, which can lower performance, especially on older machines.

So, to avoid the headache, let’s see how NVM works.



Installing NVM

Before installing NVM, you do not need a Node version installed on your machine, and, if you do have Node installed, it does not matter. Installing NVM and using it to install Node versions will work separately from the existing one.

Let’s run nvm and node on the terminal. It shows that nvm and node are not found and suggests how to install Node:

Checking That NVM And Node Are Installed In The CLI

However, since we are not installing Node directly, we’ll focus on using NVM.

To install NVM, run the following command on your terminal:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash

cURL comes with most Linux-based operating systems. If for any reason, you do not have cURL installed on your machine, you can download it from this guide.

Running the above command downloads a script and runs it.

This script downloads the entire NVM repository to ~/.nvm and adds the source lines from the snippet below to the correct shell startup script, that is, ~/.bash_profile, ~/.zshrc, ~/.profile, or ~/.bashrc, depending on the shell program you are using.

In my case, I’m using ~/.bashrc:

//source line added
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm

After that, exit the terminal and reopen it.

Run nvm, you should get the following:

NVM Installed Showcasing The Commands In The CLI

You have successfully installed NVM. Now, let’s install and manage multiple Node versions.

Installing and managing Node.js versions

To install a Node version, simply run the following command:

nvm install --<the node version>

Let’s start by installing the latest LTS version, which is version 16.13.0 at the time of writing this article. This is done by running nvm install --lts, which produces the below screen:

Running NVM To Install LTS

We now have npm version 8.1.0 as well.

After this installs, it will automatically set the default Node version to the LTS that we just downloaded.


More great articles from LogRocket:


For this tutorial, we want three Node versions running on our machine. To find the list of available Node versions:

nvm ls-remote

Running nvm install node installs the latest Node version available at the time. If we want to install version 12.22.7, we simply run nvm install 12.22.7:

Installing The Node Version 12.22.7

This downloads version 12.22.7 from the source and installs it; at the time of using 12.22.7, the npm version is 6.14.15.

Whenever you download a new version, it replaces the previous one that was in use. Let’s go ahead and install version 14.18.1 with nvm install 14.18.1:

Installing Node Version 14.18.1

Displaying a list of Node.js versions

We can now view all the versions we downloaded so far; currently, we have three Node versions installed using NVM.

To see the full list, run the following command:

nvm ls

The list then appears:

List Of Installed Node Versions, The Version Highlighted In Green Is The Currently Active Version

The first three lines show the list of Node versions with the arrow pointing to the 14.18.1 version that is currently in use; when a version is used, it displays as green.

If you happen to have more than three versions installed, of course, they will also display.

Switching among Node.js Version

The best feature about NVM is the ability to easily switch between different Node versions.
Say we must use version 16.13.0 and then switch to 12.22.7; we can simply run either nvm use 12.22.7 or nvm use 16.13.0 to easily switch into either version we need.

Switching Between Node Versions In The CLI

Note that since we only have one version that begins with 12, 14, or 16, we can switch versions with a simple nvm use 16, nvm use 14, or nvm use 12 command.

Switching Node.js Versions With Simple Version Command

Removing a Node.js version

Often, you may not need a particular version of Node for the projects you are working on. With NVM, you can easily remove the versions you don’t need.

To remove a version, just run the following command:

nvm uninstall <the version number>

The terminal will then show that the version is uninstalled:

Uninstalling Node Versions In The CLI

You must note that each version of the installation is independent, meaning that global packages on the previous versions installed will not be available on a new installation.

If you have the Gatsby CLI installed globally on version 14 of Node, for instance, when you switch to version 16 or any other version using NVM, the Gatsby CLI will not be available in the version you just switched to.

We can fix this by bringing all global packages to the new version of Node right when we first install it.

Again, know that NVM is Linux-based, meaning the installation sections and everything mentioned above will only work for Linux-based macOS or Linux-based distribution.

If you are a Windows user, check out this project by Corey Butler, which provides NVM for Windows. Everything is similar and it already has an installer here.

Conclusion

We have come to the end of this tutorial. We discussed what NVM is and its use cases, including managing multiple Node versions on one machine.

NVM is a convenient way to install and manage different versions of Node rather than installing directly, which restricts you to just one version, offering flexibility.

200’s only Monitor failed and slow network requests in production

Deploying a Node-based web app or website is the easy part. Making sure your Node instance continues to serve resources to your app is where things get tougher. If you’re interested in ensuring requests to the backend or third party services are successful, try LogRocket. https://logrocket.com/signup/

LogRocket is like a DVR for web and mobile apps, recording literally everything that happens while a user interacts with your app. Instead of guessing why problems happen, you can aggregate and report on problematic network requests to quickly understand the root cause.

LogRocket instruments your app to record baseline performance timings such as page load time, time to first byte, slow network requests, and also logs Redux, NgRx, and Vuex actions/state. .
Precious Luke Open source crusader. I love building stuff with great technology.

Leave a Reply