How to Manage Your Node.js Versions Using the Node Version Manager (NVM)

mmili_01

Abba Emmanuel

Posted on July 15, 2024

How to Manage Your Node.js Versions Using the Node Version Manager (NVM)

Certain projects require different versions of Node.js. For example, an older project might depend on Node.js 10.X, while a newer project may require Node.js 16.X. This dependency on different versions can cause compatibility issues during development.

With Node.js version managers like NVM, managing these different environments can be easier. In this tutorial, you will learn how to use the Node Version Manager (NVM) to manage your Node.js versions. You will also explore various alternatives to NVM.

Getting Started with NVM

Node Version Manager (NVM) is a free and open-source tool that simplifies managing multiple Node.js versions on a single system. It acts as a command-line interface (CLI), allowing you to easily install, switch between, and uninstall different Node.js versions.

NVM helps the user install, switch to, and remove different versions of Node.js depending on the project requirements via the command line without affecting existing Node.js versions. NVM is easy to understand and works on any Portable Operating System Interface (POSIX).

Installing NVM on Windows

NVM-Windows is a third-party tool that is not officially supported by NVM. But there's a tool created by Coreybutler that is similar to NVM but for Windows, called nvm-windows.

First, go to the GitHub page, scroll down, and click on the download now button indicated with the red arrow.

download button

Clicking on the download button will lead you to the installation page.

Click on the install.exe file.

installation file

Download the file and follow the on-screen instructions to set the installation location. You should have something similar to this:

Installation window

Finish up the setup after the download:

Installation completed

With this, the installation has been successful.

Run the command below to confirm if NVM was installed successfully:

nvm --v
Enter fullscreen mode Exit fullscreen mode

The command above should return a version number.

Installing NVM for UNIX-Based Operating Systems

Installing NVM on Mac and Linux is similar because both are UNIX-based operating systems, making the installation process similar. To install, use the command below:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
Enter fullscreen mode Exit fullscreen mode

You should get a response similar to this after running the command above:

Installation response

Test if NVM is installed properly by restarting your terminal, then run the following command to check for the version:

nvm -v
Enter fullscreen mode Exit fullscreen mode

The command above should show you the version of NVM installed.

Managing Node.js Versions With NVM

This section discusses the various ways to manage your Node.js versions on your machine. We would talk about installing, removing, listing, and setting a Node.js version as the default.

Installing a Node Version

Use the command below to install a particular version of Node.js:

nvm install <version>
Enter fullscreen mode Exit fullscreen mode

In this case, you will have to replace version with the version of Node.js you wish to download. for example:

nvm install 12.3.1
Enter fullscreen mode Exit fullscreen mode

The command above will install Node.js version 12.3.1. To check, you can run the node -v command, which will show you the version in use.

Removing a Node Version

NVM can install any particular version of Node.js; it can also delete a particular version you want. To do this, you’ll run the following commands:

nvm uninstall <version>
Enter fullscreen mode Exit fullscreen mode

Replacing the placeholder with the version:

nvm uninstall 12.3.1
Enter fullscreen mode Exit fullscreen mode

Setting Default Node Version

Run the command below to set a particular version as your default:

nvm alias default <version>
Enter fullscreen mode Exit fullscreen mode

Replacing the placeholder with the actual version:

nvm alias default 12.3.1
Enter fullscreen mode Exit fullscreen mode

Listing Existing Node Versions

NVM can list the versions of Node.js you can download on your machine using NVM. Use the command below to do this:

nvm ls-remote
Enter fullscreen mode Exit fullscreen mode

Alternatives to NVM

While NVM does a great job managing your Node.js versions on your machine, you might decide to use other packages that can do the job, and each of them has its own pros and cons.

Volta

Volta is a version manager that goes beyond the scope of Node.js, unlike NVM, to manage other Javascript tools like yarn, NPM, and even Rust. It is a good alternative to NVM, as it has the unique ability to automatically switch versions based on project requirements, which NVM doesn’t have.

Run the command below to install Volta on most Unix-based Operating Systems (macOS and Linux):

curl https://get.volta.sh | bash
Enter fullscreen mode Exit fullscreen mode

For bashzsh, and fish, this installer will automatically update your console startup script. If you wish to prevent modifications to your console startup script, see skipping Volta setup.

To manually configure your shell to use Volta, edit your console startup scripts from your terminal to:

  • Set the VOLTA_HOME variable to $HOME/.volta
  • Add $VOLTA_HOME/bin to the beginning of your PATH variable

For Windows, download and run Windows installer, then ****follow the instructions on your screen.

You can get more information on how to use Volta on the documentation

Fast Node Manager (FNM)

FNM is a cross-platform Node version manager written in Rust with speed in mind. Just as the name implies, it is faster than the NVM.

Run the command below to install FNM on macOS and Linux:

brew install fnm
Enter fullscreen mode Exit fullscreen mode

Then, set up your shell for FNM according to the documentation.

To install FNM on Windows using Winget:

winget install Schniz.fnm
Enter fullscreen mode Exit fullscreen mode

Then, set up your shell for FNM according to the documentation.

N

N is another Node.js management tool designed to handle different versions of Node.js on your machine. The main advantage of using N over NVM is that global npm packages are unaffected when switching between different Node versions.

Run the command below to install N:

npm install -g n
Enter fullscreen mode Exit fullscreen mode

If you don’t have a version of Node or NPM installed, you can install n using a Bash script from GitHub.

Here’s how to do it:

curl -L https://bit.ly/n-install | bash
Enter fullscreen mode Exit fullscreen mode

Run the command below for help when N has been installed successfully

n -h
Enter fullscreen mode Exit fullscreen mode

Conclusion

You have learned how to install and manage different versions of Node.js on your machine while choosing the one that is most compatible with your project. NVM makes your development workflow easy and keeps your environment organized.

You have also been introduced to alternatives like Volta, FNM, and N, which you can choose from based on your preferences. With the knowledge gained from this article, you can confidently choose which Node.js version manager is suitable for your project.

💖 💪 🙅 🚩
mmili_01
Abba Emmanuel

Posted on July 15, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related