Node.js NVM
Last updated onNVM (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.
After downloading the program, just double-click it.
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.
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.
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.
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.
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“.
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.
FAQs
What is NVM (Node Version Manager)?
NVM or Node Version Manager is a command line tool to manage multiple versions of Node.js on your local single machine. Once it is installed, developers can easily install, update, or switch between different Node.js versions. The service was released to support multiple Node.js versions such that each of our projects can use a specific appropriate Node.js without conflicts.
Why should I use NVM?
NVM is extra beneficial if you are a developer who is working on a number of projects that use different versions of Node.js. It eases the process of switching versions so that your development environment is consistent with the version of node importantly required for each project. It also helps with effortless versioning of Node.js which is good for testing new features or keeping old projects.
How do I install NVM on Windows?
To install NVM on Windows:
Go to the NVM for Windows GitHub page and download the installer that suits your machine’s requirements.
How do I install NVM on Ubuntu?
To install NVM on your Ubuntu OS you need to follow the following steps:
- Run this command to update the Ubuntu package
sudo apt-get update
- Install the
curl
command to help you download the NVM installer on Ubuntu using this commandsudo apt install curl
. - Start the downloading using this command
sudo curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
. - Alternatively, use wget to install NVM:
sudo wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
- load the NVM to your shell by using the following command:
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"
source ~/.profile
What are the basic NVM commands?
There are many commands on NVM to manage the Node.js versions, they are listed as the following:
nvm install <version>
: this command will help you install a specific Node.js version.nvm use <version>
: this helps you to switch to the Node.js version which is already installed on your operating system.nvm ls
: this command, lists and displays all current installed versions of Node.js and highlights the active version using the asterisk flag.nvm alias default <version>
: this command to set a target version of Node.js as a default version.
How do I switch between different Node.js versions using NVM?
To switch between Node.js versions with NVM, use the following command:
nvm use <version>
How can I list all installed Node.js versions with NVM?
You can list and display all Node.js versions using the following command:
nvm ls
How do I set a default Node.js version with NVM?
To set a default Node.js version that will be automatically used in each new terminal session, use the following command:
nvm alias default <version>
How do I manage different Node.js versions for different projects using NVM?
When you need to have different Node.js versions for different projects, it is possible to change versions through the nvm use command or create a .nvmrc file in the root partitions of each project with the specified Node.js version.
For instance, when one project requires Node.js 15.5.0, create a .nvmrc file containing:
#.nvmrs
15.5.0
Then when navigating to each directory just run this command nvm use
.
What is a .nvmrc file?
A .nvmrc file is a plain text file stored at the base of the project that defines which Node.js version is needed by the said project. Now if you go to the project directory and try to run nvm use then NVM will automatically switch to the NodeVersion defined in the .nvmrc file.
How can I check if NVM is installed correctly?
To verify if NVM is installed on your machine, run the following command:
command -v nvm
How do I uninstall a specific Node.js version with NVM?
Delete a specific version of Node.js on your operating system. You just need to run the following command with the target version.
nvm uninstall <version>
How do I update Node.js to the latest version using NVM?
To update to the latest Node.js version, run:
nvm install node --reinstall-packages-from=node
To update to the latest Long-Term Support (LTS) version, use:
nvm install --lts
What is the command to get help with NVM?
If you need help with NVM commands or options, run the following command:
nvm -h
#or
nvm --help
Wrapping Up
The article serves as an introductory manual on NVM (Node Version Manager) which is basically a program that allows users to install and utilize various versions of the Node.js program on one system. It describes the usefulness of NVM within development where different projects require different versions of Node.js so the users can quickly swap among them instead of having to reinstall NVM. The article also details the steps involved in installing NVM in Windows and Ubuntu, as well as basic operations for working with different Node.js installations, including but not limited to installing, switching, listing, cursor to the default version, and doing other projects. Other side effects of NVM are also covered and ways of dealing with them are offered. To finish, it shares tips for looking for help using the NVM command line.
Thank you for reading. Happy Coding!