What is Node Version Manager
Node Version Manager (commonly referred as NVM), is a robust tool to simplify managing multiple versions of Node.js running on a single system. Sometimes it is necessary to work on multiple projects with different Node.js versions. With NVM, switching between different versions of Node.js is easy. This helps in keeping your development environment efficient and clean.
In this article, let us provide you all the details about Node Version Manager, starting from installation, setup, usage guide, and feature overview. By the end of this article, you will be able to effectively manage Node.js versions like a pro.
Why Should You Use Node Version Manager
Node.js often releases updates with each version containing better features and enhancements. Different projects may not be compatible with all Node.js versions. Hence, Node Version Manager becomes useful in these cases. Here are some advantages of NVM:
- Isolate environments: You can prevent version conflicts by keeping projects independent.
- Upgrade and downgrade ease: You can easily upgrade or downgrade to preferred Node.js version.
- Version for all environments: You can set the default Node version for global use. Alternatively, you can specify a version within each project.
- Switch versions: You can easily switch between Node versions as and when required, depending on each project’s requirements.
What Would You Need
Before we start with NVM, let us know the prerequisites.
- Comfortable with the command line terminal
- Sudo (administrator) permissions on the device (for installation)
How to Install Node Version Manager (NVM)
How to Download and Install NVM
For installing NVM on Linux and other Unix based operating systems like macOS, execute the command:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
If you would like to use wget instead of curl, you can execute the command:
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
If you are using Windows, you can use nvm-windows as an alternative maintained version compatible with Windows. You can download the installer from the Windows GitHub NVM Repository.
How to Check If NVM is Installed Properly
Once the commands are executed, you can check if NVM is installed properly by restarting the terminal window or sourcing your profile by executing the command:
source ~/.bashrc
To check if NVM is properly installed, execute the command:
nvm --version
The output should mention the version number. If you see the version number, it means NVM has been properly installed and is now ready for use.
How can You Use NVM
Ideally, NVM is used to manage Node.js versions. Here are some basic commands to perform a few operations with NVM.
How to Install a Specific Node.js Version
If you would like to install a specific version of Node.js, execute the command:
nvm install Version
In this command, replace the word command with the Node.js version you would like to install. For example, if your preferred Node.js version is 14.17.0, execute the command:
nvm install 14.17.0
If you are unsure of the latest LTS (long term support) version, you can install the latest LTS version by executing the command:
nvm install --lts
How to View the List of Installed Node.js versions
To see all the Node.js versions installed with NVM, execute the command:
nvm ls
When this command is executed, all the versions are displayed. The highlighted version is the currently active version.
How to Switch Node.js Versions
If you would like to switch the active Node.js version to another version, execute the command:
nvm use version
Again, let’s use the same example. If you would like to switch to version 14.17.0, execute the command:
nvm use 14.17.0
You can set the default version of Node.js by executing the command:
nvm alias default version
How to Uninstall Node.js Versions
If you do not find any use for a particular Node.js version, execute this command to remove that particular Node.js version.
nvm uninstall Version
Periodically review the installed Node.js versions and remove unnecessary versions. This helps in keeping your environment clean and clears up storage space.
How to Auto-Switch to Project Node Version
To instruct NVM to automatically toggle to the version switched in “.nvmrc”, whenever you enter the project directory, add the below parameter to your shell configuration file
cd() { builtin cd "$@"; nvm use; }
This function runs the “nvm use” command every time you move to a new directory. This makes it easy to align with the specific project’s requirements.
How to Update the Default Version with .nvmrc
If you prefer a specific default Node version globally, it is possible with NVM. Set the preferred version in .nvmrc in your home directory by executing the command:
echo "version" > ~/.nvmrc
If you run the “nvm use” command without specifying any version, NVM will use the version specified in the command above if the project-specific .nvmrc file is not found.
Wrapping Up
Whether you are a developer working across different projects requiring multiple Node.js versions or just maintaining a streamlined environment, NVM is the go-to tool for you. NVM is a robust tool to manage Node.js versions. With NVM, you can switch Node.js versions easily, handle dependencies like a professional, and set project-specific configurations. Integrate NVM into your workflow today and let us know how it enhances the control you have on your projects and the ease it brought to your Node.js development practices.