What is NVM? A Guide to Installing and Using Node Version Manager (NVM)

Introduction

When I began my Frontend specialization with the ALX Software Engineering team just one month ago [June 2023], I was assigned a work that required me to use Node version 12.11.0, despite the fact that I already have Node version 20.0.0 installed. I was unsure whether I should uninstall my current Node version and install the one required by the new task. However, I had previously used version 20.0.0 for a few personal projects; therefore, if I uninstall it, I will also need to reinstall it in order to continue working on my projects, which means I will constantly install and uninstall Node versions.

That will not be at all a pleasant experience. After attempting to fix this issue for several hours, I discovered a Node versioning tool called Node Version Manager (NVM).

What is NodeJs?

NodeJs is an open-source, cross-platform JavaScript runtime environment. NodeJs is designed to build scalable network applications.

You can read more about NodeJs here.

What is NVM?

NVM stands for Node Version Manager. This allows us to install and switch between different Node versions.

As I mentioned in the introduction, I eventually reached a point where I was unable to install the anticipated node version without first deleting the one I had already installed. Therefore, NVM installs and uninstalls Node versions for you rather than you having to do it by hand. However, the advantage of this is that it leaves the other versions in place without removing them, allowing you to switch between them.

Let's say you are working on an existing project that uses Node version 12, but the Node version installed on your computer is version 10. Then you are likely to get this warning:

This project requires Node version 12

Installing NVM

To install NVM for the first time, make sure you are aware of the operating system you are working on and follow the instructions of your operating system.

Linux and macOS

To install NVM on Linux or macOS, you can use either the cURL or Wget tools.

Using cURL

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

Using Wget

wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash

These commands will download the install.sh script https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.2/ and then run it.

Follow the instructions displayed on the terminal to complete the installation.

Windows

Installing NVM on Windows is different from the above steps. To install NVM on Windows, you need to install a package called nvm-windows.

Note that nvm-windows is not the same as nvm.

To install the nvm-windows package:

  • First download the nvm-setup.zip from the nvm-windows GitHub repository:

    https://github.com/coreybutler/nvm-windows/releases

  • Navigate the downloaded zip file and extract it in any directory of your choice.

  • In the extracted directory, navigate nvm-setup.exe and run to install.

  • After installation is complete, open a new terminal or command prompt to start using nvm.

So far so good, we have been able to install the NVM using nvm-windows packager. Now let's check if our NVM is installed using the following command:

nvm --version

# or you can also use 
nvm -v

This will print the current version of the NVM you have installed on your Windows.

Once it is successful and you can see your NVM version, you are good to use nvm to install any Node version you want.

Installing NodeJs using NVM

Now that NVM is installed, we can install any particular Node version of our choice using the following command:

nvm install <version>

Replace the <version> with the Node version you would want to install, for example, "11.2.1", "16.2.3", "20.0.0", LTS, etc.

node intall 20.0.0

To install the latest version of NodeJs use the

nvm install node

# This will install the latest version of Node for you.

Switching different Node version

Great job done, take a few seconds and pat your back for getting along this far.

To switch from one Node version to the other, we use the nvm use command: Before using this command, make sure you have the version you want to switch to installed already on your system using the previous procedures.

nvm use 20.0.0

node --version
# v20.0.0

Note, if you switch to a major Node version let say version 14, without stating the full version (like "14.2.0"), nvm will switch to the latest version of the major version you types.

Example, let's say we have these versions installed "14.1.1", "14.8.2", and "14.3.1". Using the nvm use 14 will switch to the latest version of 14 installed on your system which is "14.8.2".

nvm use 14

node --version
# v14.8.2

Useful NVM commands

List

Using the nvm list command will list or display all the Node versions installed on your system and also shows you the current one you are using.

nvm list

  v20.3.1
* v20.0.0 (Currently using 64-bit executable)
  v12.11.1

So this displays all my installed Node versions in my command promt and the one with the * indicate my current Node version.

Uninstall

The uninstall command does the opposite of the install command. This is used to remove or delete a Node version from your system.

nvm uninstall 13.1.0

This will uninstall the version "13.1.0" from you system. You can use the list command to check if it uninstalled or not.

Run

If you already have a script or project installed on your computer and want to run it with a specific Node version, you can use the run command to do so without uninstalling or changing the Node version that the script or project is currently using.

nvm run 12.11.1 app.js

This command will run app.js using Node version "12.11.1".

Benefits of using NVM

  • Multiple Node.js Versions:

nvm enables you to install and maintain multiple versions of Node.js on the same machine. This is useful when working on projects that rely on different Node.js versions.

  • Easy Switching:

You can easily switch between installed Node.js versions using nvm. This allows you to test your code on different versions or use specific versions based on project requirements.

  • Global and Local Node.js Versions:

nvm allows you to set a global Node.js version for your entire system or set specific Node.js versions for individual projects.

  • NPM Version Management:

Along with Node.js, nvm also manages the version of npm (Node Package Manager) associated with each installed Node.js version.

  • Cross-Platform Support:

nvm is compatible with various operating systems, including Linux, macOS, and Windows.

  • Command-Line Interface:

nvm is primarily used through the command line, making it simple to interact with and switch between Node.js versions.

  • Community Support:

nvm is actively maintained and has a strong community, which ensures that it stays up-to-date with the latest Node.js releases and features.

Conclusion

In conclusion, I began this article by presenting Node to you, then NVM. Regardless of your operating system, I explained how to install NVM and provided you with the necessary instructions. We then covered how to use NVM to install various Node versions and how to switch between various Node versions for new or older projects.