How to Switch Node.js Versions on macOS using NVM (Step-by-Step Guide)

Switching between Node.js versions can be tricky. You’ll likely need different Node versions if you’re a developer working on multiple projects. Node Version Manager (NVM) simplifies this. This guide will clearly explain how to install NVM, manage multiple Node versions, and troubleshoot common issues on macOS.
Key Takeaways
- NVM simplifies switching between Node.js versions without conflicts.
- Ensure you source NVM correctly in your terminal profile.
- Troubleshoot common conflicts with Homebrew or global npm configurations.
What is NVM and Why Use It?
NVM is a shell script that manages multiple Node.js installations. It allows developers to quickly switch Node versions without affecting other system components. This flexibility helps when projects require different Node environments.
Installing NVM on macOS
Follow these simple steps to install NVM on your Mac:
- Download and Install NVM:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash
- Update Shell Profile:
Verify the following lines are in your
~/.zshrc
or~/.bash_profile
:
export NVM_DIR=""$HOME/.nvm""
[ -s ""$NVM_DIR/nvm.sh"" ] && . ""$NVM_DIR/nvm.sh""
[ -s ""$NVM_DIR/bash_completion"" ] && . ""$NVM_DIR/bash_completion""
Reload your shell:
source ~/.zshrc
- Check Installation:
nvm --version
Installing Latest Node.js Versions
With NVM installed, you can manage Node versions easily.
- Latest LTS Version:
nvm install --lts
- Latest Current Version:
nvm install node
- Specific Node Version:
nvm install 18.17.1
- List Installed Versions:
nvm list
Switching Between Node.js Versions
Use nvm use
to switch versions:
nvm use 18
Confirm your active Node version:
node -v
Set default Node version for new terminals:
nvm alias default 18
Troubleshooting Common NVM Issues
NVM Command Not Found
- Verify NVM is loaded correctly by checking your profile (
~/.zshrc
or~/.bash_profile
). - Reload your profile with
source ~/.zshrc
.
Node Version Not Switching (Homebrew Conflict)
Check conflicts with Homebrew installations:
which -a node
Remove conflicting Node installations:
brew uninstall --ignore-dependencies node
NVM Warning (npm prefix
issue)
Remove npm global prefix setting:
npm config delete prefix
Conclusion
Using NVM simplifies managing multiple Node.js versions on macOS, letting you switch versions easily and avoid version conflicts. Proper setup and addressing common issues ensure smooth usage.
FAQs
This typically means NVM isn't loaded in your shell profile correctly. Verify NVM setup lines in ~/.zshrc or ~/.bash_profile and reload the shell.
Run 'nvm uninstall ', replacing with the Node version you wish to remove.
Yes, NVM automatically installs the npm version compatible with your chosen Node.js version.