As a developer working with Node.js, we might need to work on various projects and each project has its configuration. So maybe some of the projects have different versions of Node.js, and then we need to switch between different versions of Node.js for multiple projects. The Node Version Manager (NVM) is a fantastic tool that makes this process easier. In this guide, we'll cover three ways to install NVM on macOS and show you how to manage multiple Node.js versions with ease.
What is NVM?
NVM is a version manager for Node.js, designed to simplify the installation and management of multiple Node.js versions on a single machine. With NVM, you can easily switch between different versions of Node.js as per your project's requirements.
Prerequisites
Before we start, ensure you have the following:
- A macOS machine ( This method support for all versions of macOS including M1, M2 and Intel)
- Command Line Tools (you can install them by running
xcode-select - install
in the Terminal) Now, let's explore the three ways to install NVM on macOS:
Method 1: Installing NVM Using the Curl Command
This is the recommended method from the official NVM GitHub repository.
- Open Terminal: Launch the Terminal application on your macOS.
- Download and Install NVM: Run the following command to download and install NVM:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
-
Load NVM: After installation, you need to load NVM into your current terminal session. Add the following lines to your shell profile file (e.g.,
.zshrc
for Zsh or.bash_profile
for Bash):
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
- Apply Changes: Run the following command to apply the changes:
source ~/.zshrc
- Verify Installation: Check if NVM is installed correctly by running:
nvm - version
Method 2: Installing NVM Using Homebrew
Homebrew is a popular package manager for macOS.
- Open Terminal: Launch the Terminal application.
- Install Homebrew (if not already installed):
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
- Install NVM with Homebrew: Run the following command to install NVM:
brew install nvm
- Create NVM Directory: Create a directory for NVM:
mkdir ~/.nvm
-
Load NVM: Add the following lines to your shell profile file (e.g.,
.zshrc
or.bash_profile
):
export NVM_DIR="$HOME/.nvm"
. "$(brew - prefix nvm)/nvm.sh"
- Apply Changes: Run the following command to apply the changes:
source ~/.zshrc
- Verify Installation: Check if NVM is installed correctly by running:
nvm - version
Method 3: Installing NVM Using MacPorts
MacPorts is another package management system for macOS.
- Open Terminal: Launch the Terminal application.
- Install MacPorts (if not already installed): - Download and install MacPorts from the official MacPorts website.
- Install NVM with MacPorts: Run the following command to install NVM:
sudo port install nvm
-
Load NVM: Add the following lines to your shell profile file (e.g.,
.zshrc
or.bash_profile
):
export NVM_DIR="$HOME/.nvm"
. /opt/local/share/nvm/init-nvm.sh
- Apply Changes: Run the following command to apply the changes:
source ~/.zshrc
- Verify Installation: Check if NVM is installed correctly by running:
nvm - version
Managing Multiple Node.js Versions with NVM
Now that you have NVM installed, let's see how you can manage multiple Node.js versions.
Installing a Specific Node.js Version
To install a specific version of Node.js, use the following command:
nvm install <version>
For example, to install Node.js version 14.17.0:
nvm install 14.17.0
Listing Installed Node.js Versions
To list all installed Node.js versions, run:
nvm ls
Switching Between Node.js Versions
To switch to a specific version of Node.js, use the following command:
nvm use <version>
For example, to switch to Node.js version 14.17.0:
nvm use 14.17.0
Setting a Default Node.js Version
To set a default Node.js version, which will be used whenever you open a new terminal session, run:
nvm alias default <version>
For example, to set Node.js version 14.17.0 as the default:
nvm alias default 14.17.0
Conclusion
With NVM, managing multiple Node.js versions on macOS is a breeze. Whether you choose to install NVM using the curl command, Homebrew, or MacPorts, you now have the tools to easily switch between Node.js versions and ensure your projects run smoothly. Happy coding!
Top comments (0)