How to Install NVM on Mac
Problem
If you manage many node.js projects, you will definitely come across a time when your packages require you to use an upgraded node version. I think that using NVM is the best bet for this task. NVM is a version manager for node.js. You can easily install new node versions and switch around different versions.
I find online that many suggest using homebrew to install NVM. However, it is NOT a recommended way to install NVM according to their official document.
How to Install
This one line of command is the easiest way to install or upgrade to the latest nvm version.
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
This command will download an install script to clone the nvm package to your home directory ~/.nvm
. After finishing the install, the script will automatically insert the following profile snippet to your profile ~/.zshrc
file.
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
If the profile snippet is not stored to your profile successfully or the script does not work, you can try manually copying this snippet to the bottom of ~/.zshrc
file by yourself.
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
How to know if NVM is successfully installed
Restart your terminal. And type nvm --version
. This will show an installed nvm version. That's it.
Now you can enjoy the power of NVM. What can you do after your installation?
Install a specific node version, nvm install 18.19.0
Select to use the installed version, nvm use 18.19.0
List all locally installed node versions, nvm ls
To learn more details of NVM, visit their github page.
Happy Coding!