Mastering Node.js Version Management with nvm: A Comprehensive Guide
Aymen kani|January 10th 2023

As a JavaScript developer, you have probably encountered situations where you need to use different versions of Node.js for different projects. Maybe you are working on a legacy project that requires an older version of Node, or you want to test your code on the latest version to see if it is compatible. In these cases, it can be useful to have a tool that allows you to easily switch between different versions of Node. This is where nvm, the Node Version Manager, comes in.

What is nvm?

nvm is a command-line tool that allows you to install, manage, and switch between multiple versions of Node.js on your local machine. It is particularly useful for developers who need to work with multiple Node projects that have different version requirements.

Installing nvm

To install nvm, you will first need to install the dependencies required by nvm. On macOS and Linux systems, this can be done by running the following command:

bash

sudo apt-get install build-essential libssl-dev

On Windows systems, you will need to follow this guide: nvm window installer

Once the dependencies are installed, you can install nvm by running the following command:

bash

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

This will download and run the nvm installation script. Once the installation is complete, you will need to close and reopen your terminal window in order for the changes to take effect.

Using nvm

To see a list of all the available versions of Node.js that you can install with nvm, run the following command:

bash

nvm ls-remote

This will display a list of all the versions of Node.js that are available to install. To install a specific version of Node.js, use the following command:

bash

nvm install <version>

For example, to install version 12.16.1 of Node.js, you would run:

bash

nvm install 12.16.1

This will download and install the specified version of Node.js. Once the installation is complete, you can switch to the new version of Node.js by using the following command:

bash

nvm use <version>

For example, to switch to version 12.16.1 of Node.js, you would run:

bash

nvm use 12.16.1

You can also set a default version of Node.js to use when you open a new terminal window by running the following command:

bash

nvm alias default <version>

For example, to set version 12.16.1 of Node.js as the default, you would run:

bash

nvm alias default 12.16.1

Uninstalling nvm

If you decide that you no longer need nvm, you can uninstall it by running the following command:

bash

rm -rf ~/.nvm

This will delete the nvm configuration files and remove it from your system.

Conclusion

nvm is a powerful tool that can make the process of managing multiple versions of Node.js much easier. It allows you to install, manage, and switch between different versions of Node with a few simple commands, making it ideal for developers who need to work on multiple projects that have different version requirements. By using nvm, you can ensure that you always have the right version of Node installed for each project, and you can easily test your code on different versions of Node to ensure compatibility.

Read next

Node.js nvmversion managementJavaScriptNode Version Managerdevelopmentworkflowefficiency