When working with NodeJs applications we may want to use different Node versions but this is a cumbersome task and here comes the use of NVM.
NVM as the name says Node Version Manager, is a version manager for node.js which allows you to easily switch between Node versions, which is essential. You can install different versions of node and switch anytime between those versions with just a command line.
Installing NVM on mac OSX
installing is pretty simple just you need to run the install script as shown below
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
The above command downloads a script and runs it. The script clones the nvm repository to ~/.nvm
, and attempts to add the source lines from the snippet below to the correct profile file (~/.bash_profile
, ~/.zshrc
, ~/.profile
, or ~/.bashrc
).
Checking NVM install
To check wether NVM was installed properly, try to run below command
nvm --version
You will get to see the nvm version installed on the system as shown in the below screenshot.
Troubleshooting
After installing nvm if you get to see nvm not found error as shown below
This could be due to possible due to 2 problems:
- Either NVM is not installed properly
For this go to your home folder (~ or $HOME) and check if a .nvm folder exists there.~/.nvm/nvm.sh
- NVM path is not set
For this open (~/.bash_profile
,~/.zshrc
,~/.profile
, or~/.bashrc
) whichever shell you are using and to the bottom of the file add below line.source ~/.nvm/nvm.sh
Installing Node
To install the latest node version, run the below command
nvm install node
To install the recent LTS node version, run the below command
nvm install --lts
To install any specific version let’s say 12.18.3 in our case, run the below command
nvm install 12.18.3
Other NVM commands
To check the installed node version via NVM
nvm ls
Switch between node versions, let’s say we want to switch to 12.18.3
nvm use 12.18.3
NVM also has a default version which means that if you start your terminal fresh instance then your default node version will be used automatically.
By default, the 1st installed node is your default node version in NVM.
To change your default node version in NVM, let’s say we want to set 12.18.3 as the default node, then run
nvm alias default 12.18.3
Let me know your thoughts