Node Version Manager (NVM): Manage Node.js Versions

NVM (Node Version Manager ) is a command-line tool used to manage multiple versions of Node.js on a single machine. It makes installing, updating, and switching between many different Node.js.

This tool is very useful for developers who work sometimes on multiple projects and are required to test their new code on different Node. js versions.

So, let’s take a look at the definition of NVM and why we should use it.

Node Version Manager Definition

The Node Version Manager is the right way to deal with managing many Node.js environments.

It allows to set up multiple Node.js versions on their system and can quickly switch to different versions if needed.

This leads to the ability to ensure that your development environment matches exactly what version of Node.js is being used.

Using NVM with Node.js has several major advantages. It allows you to keep various sets of Node.jsRunning multiple versions of Node.js for different projects so that each project runs in an environment where there are no conflicts with other applications. That can be beneficial when working on legacy projects that need an older Node.js version or trying the new feature out on the latest version for testing updates. On the other hand, NVM makes Node.js upgrade/downgrade easy.

This installs each version of the Node.js that you choose so that you can play with newer releases without worrying about breaking your current projects. To put it briefly, NVM really makes handling your Node.js flexibility-control-convenient. A must-have tool for any Node.js environment js developer.

Anyway, let’s move into the following section to learn how to install NVM on various operating systems.

Installing Node Version Manager (NVM) on Windows

To install NVM on Windows, go to this link and download the program that meets your machine’s requirements.

Then, follow the following steps one by one.

nvm github

After downloading the program, just double-click it.

NVM for windows

In the above screen, you may see the license agreement screen in the image above. Simply choose – “I accept the agreement“ to accept the conditions of the license.

In the following stage, just press the – “next“ button.

nvm for windows

On this screen, you have to choose the installation path or leave it as the default location.

Once the – “next“ button appears, just click on it.

The figure below indicates that in order to add a – “symlink“, you just need to choose the path for the Node.js folder.

If the default location path is correct for Node.js, you can leave it. Otherwise, choose the new location path.

nodejs symlink

Installing NVM on Ubuntu

In this part, we will focus on the Ubuntu operating system, which means it requires additional tools to help us download the NVM package.

The first package we can use is – “curl.” If you don’t have – “curl“, just open your terminal and write the below command to install it on your operating system.

But before you do that, just update the Ubuntu package by using the below command.

sudo apt-get update

Then run the below one to setup the – “curl” package.

sudo apt install curl

Well done! In the following step, we simply need to download the NVM package into our operating system, and that can be done using the following command:

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

Also, if you are not familiar with the – “curl“ package or don’t have the installation, an alternative is to use the – “wget“ package. Let’s download it.

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

If you run one of the previous commands, it will download the NVM package onto your machine. But there are another two steps: setting up the NVM directory and loading its functionality into your current shell session.

That can be achieved with the below commands.

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
source ~/.profile

Basic NVM (Node Version Manager) Commands

Using NVM, managing Node.js This way working with multiple versions will be easier and you can improve your workflow ability by going in much deeper. However, here are a few important NVM commands that everybody must be aware of.

Installing and Switching Between Node.js Versions

If you already have a specific version in Node.js like this one 18.5.0 you just need to write it beside this command nvm install {your-node-version-here}. So here is an example:

nvm install 18.5.0

So, once the installation process is completed, you just need to switch your current version of Node.js to the new one. And that can be done using this command nvm use {your-node-version-here}. Here is an example:

nvm use 18.5.0

This command will make the version you specified active Node.js for your current session.

Listing Installed Node.js Versions

If you already have many versions of Node.js and need to show them on your terminal you just need to run the following command.

nvm ls

This command will display all Node.js versions on your operating system. So that will help you to select, what version you need to active.

Setting a Default Node.js Version

You can set a default Node.js version that will be automatically used in each new terminal session with nvm alias default {your-node-version-here}. Here is an example:

nvm alias default 18.5.0

This command will make the default version of Node.js 18.5.0 every time you open the terminal or the command line.

Now let’s move into the below section to learn how to manage versions in a machine like updating, deleting, and installing using the NVM command.

Managing Node.js Versions with NVM

NVM command makes it very simple, so you can install, delete, or update any version of Node.js on your operating system as needed. Here are commands to do that.

So, to install the Node.js version as I mentioned before, use the following command:

nvm install <VERSION>

Also, you can update the current version of Node.js to the latest version. You can do that by running the following command:

nvm install node --reinstall-packages-from=node

Alternatively, if you want to update to the latest LTS (Long Term Support) version, you can use:

nvm install --lts

To remove a specific Node.js version from your operating system. Execute this command:

nvm uninstall <version>

That’s it. However, if you have multiple projects that are based on different Node.js versions, how can you manage them? Let’s move into the following section to answer this question.

Using NVM with Different Projects

When working on several projects that require different Node.js versions, being able to switch the version of Node.js per project is essential. NVM makes this easy to do especially when using .nvmrc files which specify which version of Node.js a project is to use.

Project-Specific Node.js Version Management

NVM enables you the ability to switch between versions at once. So suppose you have a project based on Node.js version 15.5.0 and another one requires version 18.6.0. You can easily manage them by using this command for 15.5.0.

nvm use 15.5.0

And this command for v18.6.0.

nvm use 18.6.0

for the second. This ensures that the correct Node.js version is active for each project, avoiding any compatibility issues.

Using .nvmrc Files to Specify Node.js Versions

You can make a specific version for each project in an easier way which is creating a file .nvmrc for each project in the main folder. It should include the Node.js version that the project needs. here is an example:

#.nvmrc
15.5.0

Additionally, when you navigate to this project, you just need to run the following command:

nvm use

NVM will automatically read the .nvmrc file and switch to the specified Node.js version.

So you have to do the same thing in the other project. Create .nvmrc with the target version 18.6.0 and then when you go to this directory just run this command nvm use.

Anyway, in the following paragraphs, you will see real examples of how to use NVM with your Node.js Projects. Let’s move on.

Verifying NVM Example

Checking if NVM is installed on your machine is very simple.

command -v nvm

By executing this command, it will print the path of the NVM system on your machine.

command -v nvm

Example of Listing All Node.js Versions with NVM

To display all of the Node.js versions that are installed on your machine, just execute the following command:

nvm ls

The output will show you all Node.js versions. The current active version will be highlighted with an asterisk.

nvm ls

The Common NVM Issues

If you encounter any issue during the switching process, such as – the “access denied“ message, simply rerun the command line in administration mode by selecting – “Run as Administrator“.

Another common issue is like the one below, which is – “The current directory is not empty“.

nvm use command

.To solve this issue, you have to open the path of the Node.js folder, which is commonly located on this path, – “C:\Program Files\nodejs “ and then change the – “nodejs“ folder name to – “nodejsx“. To be like this, – “C:\Program Files\nodejsx” then rerun the command again.

Anyway, let’s explore how to access help within the NVM command.

Getting Help with NVM

If you find yourself unfamiliar with NVM commands or need help, simply run the following command:

nvm -h

OR

nvm --help

It will show all options associated with the NVM command, as displayed on the following screen.

nvm --help

    Previous Article

    Installing Node js on Windows, Ubuntu, and MacOS

    Next Article

    Node js NPM: How to Publish and Install JS Packages

    Write a Comment

    Leave a Comment

    Your email address will not be published. Required fields are marked *

    Subscribe to Get Updates

    Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
    No spam. Unsubscribe anytime.