We often need to work with multiple node.js based projects that may require different node versions to work on. Building and testing these products on a single computer working with a specific node version often gets cumbersome. I had been facing similar problems, that ended up working on a node version 16 project on one computer, and node version 20 project on another. Even more complicated at times, while uninstalling node version 16, re-installing node version 20, and so. Here’s when I got to know of the amazing tool NVM. That greatly eases use of multiple versions of Node.js on your computer by just a single command!
NVM Manager
Node Version Manager is a wonderful tool, very useful for developers who work on multiple projects that require different versions of Node.js, and often need to switch between those versions during development and testing. It greatly helps manage multiple versions of Node.js on your computer.
The tool allows you to install and maintain Node.js versions on a single machine, and switch between those versions using a single simple command. It comes with a single command line interface (CLI), that is easy to install, switch, and manage Node.js versions without manually downloading and configuring each version. So, simple install NVM and use it to install any Node.js version and switch between them.
Installing NVM
To install Node Version Manager on Windows, follow the steps below. Open this nvm-windows GitHub repository and download the latest nvm-setup.zip file from the releases section. Then extract the nvm-setup.zip and run the nvm-setup.exe installer, and follow the installation wizard instructions.
Set Environment Variables
Next you need to set up the Environment Variables to use nvm from your command line. So, open Command Prompt, (cmd.exe), choosing ‘Run as Administrator’ and first set the NVM_HOME environment variable following the path where your nvm is installed.
So, cd to C:\Users\’Your PC Name’\AppData\Roaming\nvm and set your nvm using the command below.
set NVM_HOME=C:\Users\'Your PC Name'\AppData\Roaming\nvm
Then verify your installation process, using a new Command Prompt to run the following:
nvm version
You will be able to see the version number of nvm in the console. You can now play around with your nvm tool.
To view all Node.js versions installed on your system, use the command below:
nvm list
To install a specific Node.js version, use the command below.
nvm install 16.1.0
To view all available Node.js versions, use the command below.
nvm list available
This will show all available versions your nvm tool can install.
Switch between Node.js versions
A simple command will make your computer switch to different installed versions of Node.js
nvm use 20.1.2
Change the version number with any version you want to use. Make sure the version you are entering is installed in your computer, or else do it using your nvm using the single install command shown above.
You can also set a default version using nvm, using the command given below.
nvm alias default 20.1.2
Benefits of NVM
Flexible: Allows easy switching between Node.js versions as per project requirements.
Compatible: Assures compatibility with multiple frameworks, libraries, and tools that may need specific Node.js versions.
Isolation: Segregates projects as per their Node.js versions to avoid dependency conflicts.
Testing: Allows testing applications across various Node.js versions to assure performance and compatibility.
To conclude, NVM (Node Version Manager) simplifies managing multiple Node.js versions, and makes it an invaluable tool for developers that need to work on diverse projects that require flexibility and compatibility with different Node.js environments.