If you are using a Mac, you most probably use Homebrew to manage your Node.js installation. However, if you want to use different Node versions for different projects, using Node Version Manager (NVM) to switch between versions would make your life easier.
- Remove existing Node installed via Homebrew
This is important to make sure all your Node installations are managed through NVM.
# ignore other artefacts which may depend on node
brew uninstall — ignore-dependencies node# remove all versions of node
brew uninstall --force node
2. Install NVM using Homebrew
brew install nvm
If you run into an error such as ‘Permission denied @ apply2files’, you may need to reset permissions for /usr/local as mentioned in this SO answer.
sudo chown -R $(whoami):admin /usr/local/* \
&& sudo chmod -R g+rwx /usr/local/*
3. Set NVM home directory
Create NVM’s working directory.
Edit your shell config file to add the path to the NVM home directory. The shell config file would be ~/.bash_profile if you are using Bash, or ~./zshrc if you are using Zsh, the default shell for MacOS Catalina and later versions.
# open config file
Add the following to the config file.
# add the nvm path
# load the nvm script every time you open the terminal
source $(brew --prefix nvm)/nvm.sh
Close your nano editor and verify the path to the home directory.
source ~/.zshrc # reload config file
echo $NVM_DIR # echo path
nvm --version # check nvm version
4. Install Node using NVM
nvm ls-remote # see the available node versions
nvm install node # install latest node version
nvm install 10 # install Node.js 10.x version
nvm ls # check locally installed versions
After installing, simply switch to the Node version you need to use.
nvm use 10
And that’s it!
Thanks for reading!