documentation/blog/2024-03-27-change-node-version.md
Changing Node.js versions was always difficult for developers. Due to version compatibility, changing Node.js versions for the current session was common but tedious. The end of 2023 release of Node version manager solves this problem. This article shows how to use NVM to check and modify Node.js version. We'll explain how to install and use NVM to swap Node.js versions. Let's understand what is an NVM and how it boosts developer productivity.
Of course, it is also possible to install different versions of Node.js without using NVM and you can also switch different versions without NVM. However, it will require frequent modification to PATH variable to point to the directory of a specific Node.js version. However, using NVM gives the flexibility to switch Node.js versions with a simple command.
apt for Ubuntu, brew for macOS, etc.).bash, zsh, ksh, or dash as your shell. If you’re using Windows, you can use WSL which provides a Linux-compatible shell environment.curl or wget, tar, etc.). However, the compatibility might not be as robust as macOS and Linux.curl or wget to download the script. Here’s the command using curl:curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
Or you use wget:
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
If you do not wish to use either wget or curl, you can also download the script from here and then install the script.
Here is what you will see if you execute the above curl command:
wget or curl command downloads the script and runs it in one go. The script clones the NVM repository to ~/.nvm, and adds the source lines to your profile (~/.bash_profile, ~/.zshrc, ~/.profile, or ~/.bashrc).command -v nvm
This command should output nvm, indicating that NVM is installed and accessible. You can also run the command nvm to see the version of nvm and its usage details.
command not found after running command -v nvm, this means that the terminal can’t find the nvm command. You may need to close and reopen your terminal or restart your computer. Below screenshot confirms this scenario. Closing and re-launching the terminal solves this issue though.curl or wget, you’ll need to install one of them to download the NVM installation script.You can install the latest version of Node.js using NVM without knowing the exact version number. NVM provides a special command for this purpose. Here’s how you can do it:
nvm install node
The nvm install node command will fetch the latest version of Node.js and install it. For example, the latest version is 21.7.1. After the installation, the latest version will automatically become the active version in your current terminal session, this is evident from the last line in above screenshot where nvm is setting the installed Node.js version as default Node.js version.
To install a specific version of Node.js, use the nvm install command followed by the version number. For instance, to install Node.js version 14.15.1, you would use the following command:
nvm install 14.15.1
The nvm install command downloads the specified version of Node.js and npm, allowing you to use them immediately.
NVM provides a straightforward way to view all installed Node.js versions. The nvm ls command displays a list of installed versions, with the current version highlighted:
nvm ls
After installing the latest version of Node.js (which is 21.7.1 as of writing this), this is what the above command shows:
Looking at the above screenshot, the terms “stable”, “default”, and “current” have specific meanings:
nvm use to switch to a different version. You can identify this version by --> sign in the above screenshot.In above screenshot, the version 21.7.1 is the current version as well as default and stable version. After installing 14.15.1, it will become the current version instead of 21.7.1, however 21.7.1 will remain as default. See below screenshot after running the nvm ls command after installing version 14.15.1 on top of 21.7.1.
In above screenshot, 14.15.1 is current version, but stable and default versions are still 21.7.1. If another stable version 21.7.2 is released tomorrow, then 21.7.2 will be shown as stable version and 21.7.1 will be shown as default version.
To view all available versions for installation, use the nvm ls-remote command:
nvm ls-remote
This command fetches and displays a list of all Node.js versions available for installation.
Changing Node.js versions is as simple as using the nvm use command followed by the version number. For example, to switch to Node.js version 21.7.1, you would use:
nvm use 21.7.1
This command makes the specified version the active Node.js version in your current terminal session. If you close the terminal, the effect of above command will be nullified and default version of Node.js will become active.
You can use the same command nvm use <older-version-number> to revert to an older version for this session. For example, nvm use 14.5.1will revert your current Node.js version to 14.5.1 which is much older version. That older version will of course need to be installed first.
The NVM manager is intelligent enough to use the correct Node.js version even if you use just the prefix of the version number. For example:
nvm use 14 will work as nvm use 14.5.1 as there is only one installed version that started with 14nvm use 21 will work as nvm use 21.7.1 as there is only one installed version that started with 21If you want to use a version as current version even after closing a terminal, you can change your default version by using the below command nvm alias default 14.5.1 where you can replace 14.5.1 with any Node.js version you want to be default.
In above screenshots, you noticed that the current version of Node.js is shown by --> in the output of command nvm ls. Another way to verify the current version of Node.js using NVM with the nvm current command. This command will display the version of Node.js that is currently in use in your terminal session. Here’s how you can use it:
nvm current
This command will output the version of Node.js that is currently active. For example, it might output v14.15.1 if that’s the version you’re currently using. If no version is active, it will output system, indicating that the system’s default Node.js installation is currently in use.
If you have read this article, you are now expert in using NVM to change your Node.js version. Using a version manager like NVM improves the efficiency of developers and simplify the development process. Using NVM, not only you can specify any Node.js version for your current session, but you can also modify the system's default Node.js version as well.