Did you know you can have different versions of Node on your computer? Some projects could be using a different version of Node and you might run into problems when running these projects. You might have used npm to install or uninstall these versions but there is a better way to manage Node versions. In this topic, we will talk about Node Version Manager(NVM). You will go through the installation of NVM and learn how it can manage different Node versions on your computer.
Installing NVM
NVM works on any of these shells: sh, dash, ksh, zsh, bash, and on these platforms: UNIX, macOS, and Windows WSL.
To install, run the curl command shown below:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
Or the wget command:
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
These commands clone the nvm repository to ~/.nvm and attempt to add the nvm configuration to your profile file. The profile file may be ~/.zshrc or ~/.bash_profile depending on your shell.
If you don't see the configuration lines in your profile, add them manually:
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"
To verify the installation, run:
nvm -v
Or the full command:
nvm --version
With either of the commands, output should be something like: 0.38.0 .
If you get nvm: command not found, close your terminal. Then, try the same command with a new terminal.
NVM on windows
NVM doesn't have official support for Windows, but there's a similar tool called nvm-windows. The installation process for the tool is straightforward.
- Go to the releases page in the repository,
- Download the
nvm-setup.exefile from theAssetssection and follow the installation wizard, - Use the shell as Admin to run
nvm-windows. To use shell as an admin, startpowershellor Command Prompt with administration privileges.
You can verify if the installation is successful with the following commands:
nvm -v
Or the full command:
nvm --versionUsing NVM
After installing NVM, you can use the following command to install the latest version of Node:
nvm install node # "node" is an alias for the latest version
To install a particular version, run the command given below:
nvm install 14.7.0 # or 16.3.0, 12.22.1, etc
To make a version default, use the following command instead:
nvm alias default 14.7.0 # not available on nvm-windows
To list all of the installed versions, you can run:
nvm ls
Or the full command:
nvm list
The following command will list all of the available versions:
nvm ls-remote
Or the full command:
nvm list-remote
On nvm-windows run the following:
nvm list available
If you want to use a different version for the current terminal, you can execute the following command:
nvm use node
Now using node v16.14.2 (npm v8.11.0)
Or a specific version:
nvm use 16.14.2
Now using node v16.14.2 (npm v8.11.0)
This command will be helpful when you want to run a project with a different Node version.
nvm to install, uninstall, and manage different npm versions.NVM vs NVM-Windows
The biggest use case difference between the two is that with nvm-windows, you don't need to run nvm use x.x.x with different terminal sessions. Because nvm-windows automatically updates and retains the default version of Node on all open terminals when you run this command. This means that there is no extra command to make a version default like: nvm alias default x.x.x.
Also in nvm-windows, you can choose to use/install either the 32 or 64-bit mode:
nvm install x.x.x [arch]
nvm use x.x.x [arch]
Another difference is you can't use nvm-windows to manage different npm versions.
Conclusion
When working on Node projects, some of them can use different versions of Node and this could cause issues when running projects. Or you might download a Node project and see that it uses a different Node version. To easily switch between or install new Node versions, you can use Node Version Manager. In this topic, you've looked at installing and using Node Version Manager for different platforms.