If you are working with different projects that require use of different NodeJS versions. Node Version Manager (NVM) is what you are looking for. It allows you to manage different node version on single machine. By using NVM multiple version of Node.js can be installed and switch between versions at disposal.
Using nvm you don’t have to install stable Node.JS if you would like to test your project against bleeding edge version of Node.js
Install Node Version Manager(NVM) on windows
These are several node version manager like nvm and n but they are not promising for windows. Thus, nvm-windows is developed solely to support windows
Following the steps mentioned below to equip your system with multiple versions of Node.js using nvm-windows
- Uninstall the installed Node.JS: Uninstall existing Node.js version before proceeding. Also delete if there are any left over directories of installation directories (e.g., C:\Program Files\nodejs) to avoid any problems .Remove (or rename to npm_old) if there are any left over npm directories from installation locations e.g., C:\Users\<user>\AppData\Roaming\npm. Take a backup before deleting if you have any important npm configuration
- Download nvm: if not downloaded already, download nvm for windows by choosing latest stable nvm-windows release .
- Once nvm-setup is downloaded, extract and double click on nvm-setup.exe to proceed with installation
Installation steps of nvm
After installation verify using
nvm --version
If you wanted to check the which version of node is being used
nvm version
Install Node Version Manager(nvm) on Linux/Mac
Unlike Windows Linux users have several version managers for Node.js to choose from like nvm and n. Install nvm on Linux as follows using install script using curl
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash
Or, using wget
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash
Either of the above command would install nvm on Linux. But, to make it work edit the shell’s startup files (~/.bashrc, .bash_profile, .zshrc, ~/.zshrc, ~/.profile)
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
Use .nvmrc file to specify Node.js version for project in project root directory. Where, commands “nvm run” and “nvm exec” can be used seamlessly where they will choose desired Node.js version from .nvmrc
Command syntax for nvm and n for Linux is different compared to nvm-windows. Refer command nvm command usage for Linux
Install Multiple Node.js Versions using nvm-windows
Install the desired version using “nvm install” command. For example to install version “12.18.3”
nvm install 20.9.0
List the available versions to be installed using command “nvm list available”
nvm list available
This command will display version of Node.js to be installed in tabular form organized as “current”, “LTS”, “OLD STABLE” and “OLD UNSTABLE”
nvm list avaiable
has been changed, to list all version not install use the command
nvm ls-remote
List the only LTS (Long Term Support) version
nvm ls-remote --lts
List Install Node.js Version
List the installed version of Node.js using command “nvm ls” or “nvm list”
nvm ls
switching Between Version
Use “nvm use” command to switch to desired version. For example to switch to version “12.18.3”
nvm use 12.18.3
Switching between versions became this simple using nvm-windows. Switching as many times as you need
C:\>
C:\>nvm use 10.22.0
Now using node v10.22.0 (64-bit)
C:\>
C:\>nvm use 12.18.3
Now using node v12.18.3 (64-bit)
C:\>nvm use 14.19.0
node v14.19.0 (64-bit) is not installed.
Switching to version that is not available is ignored with message <version> is not installed.
It would be tedious and time consuming to switch between versions while you are working on multiple version for different projects. In such case place .nvmrc configuration file with just version number of node in root folder of project directory structure.
Uninstall Node.Js Version
To remove unused installation of Node.js version use the following command with version
nvm uninstall 10.18.1
Conclusion
Often times it is frustrating if different projects require different versions of Node since we need to uninstall old version and install new Node to switch. Node version Manager (NVM) become the savior in such a situations, it can be used to install multiple Node.js version and switch between them. The choice of Node version for the project can also be made implicit by using .nvmrc file in project root
Leave a Reply