Efficiently manage multiple Node.js versions: NVM installation guide for Mac system
Managing multiple Node.js versions simultaneously, especially when one project requires version 14 and the other requires version 16, it may feel like walking a tightrope. Believe me, I have a deep understanding - I once stayed up late to debug error messages for the project's requirements for the Node.js version. At this time, NVM (Node version manager) comes in handy.
Whether you are a developer running legacy projects, a developer trying the latest features, or a beginner who is learning Node.js for the first time, NVM ensures your productivity. You can think of it as your Node.js "time machine" that allows you to switch freely between different versions.
If you have similar problems, please let me take you to install NVM on your Mac system step by step. I will break all the steps down in great detail to make it easy for you to complete the installation.
Before installing NVM, let's quickly review some preparations to ensure the installation process goes smoothly.
You need:
Also, check out how to uninstall Node.js on your Mac.
The most popular and straightforward way to install NVM is to use curl (a common tool already installed on most Macs) or wget (another tool for downloading files). This is the way I tried first, as it looks fast and I'm eager to fix the Node.js issue in my project.
Note: curl and wget? Just use any available tools. curl is pre-installed on macOS, but you can install wget via Homebrew (brew install wget) if needed. If you are not sure which shell you are using, run echo $SHELL. Starting with macOS Catalina, Zsh is the default shell, so most users will edit ~/.zshrc.
Before we understand the installation process in depth, let's first understand the operation process of the terminal, especially for new users of macOS Sequoia. This is not difficult.
Operation steps:
curl -o- https://raw.githubusercontent.... | bash
wget -qO- https://raw.githubusercontent.... | bash
nano ~/.zshrc
nano ~/.bash_profile
export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
source ~/.zshrc
(If you are using Bash, replace ~/.zshrc with ~/.bash_profile). nvm --version
. Congratulations if you see the version number! You have successfully installed NVM.
Personal Experience: When I first installed NVM, I encountered a problem: the Xcode command line tool is missing. Quick solution: xcode-select --install
. After running the curl command, adding NVM to my shell configuration file, and applying the changes with source ~/.zshrc
, everything worked fine.
If you like to save time and simplify operations, Homebrew is your best choice when installing NVM. It's like the Swiss Army Knife of macOS tools - simple, reliable, and can handle all the tedious things for you. If you want to escape at the thought of copying messy scripts, Homebrew is your new best partner. It can complete tasks quickly and give you more valuable time.
I fell in love with it the first time I used Homebrew. It feels like finding a cheat code for your Mac. Trust me, once you try it, you’ll want to know how you survived without it before.
The following is how to install NVM using Homebrew:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
. brew install nvm
. mkdir ~/.nvm export NVM_DIR="$HOME/.nvm" . "$(brew --prefix nvm)/nvm.sh"
. nano ~/.zshrc
nano ~/.bash_profile
export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
source ~/.zshrc
(If you are using Bash, replace ~/.zshrc with ~/.bash_profile). nvm --version
. If the version number appears, congratulations - you have successfully installed NVM using Homebrew!
If you are familiar with MacPorts, it is also a good choice for installing NVM. It keeps things simple and relieves the pressure to manage software on your Mac. I've used it before on older macOS versions and while it's not the most beautiful tool, it's reliable and actually helps you with what you need to do. Sometimes, this is the most important thing.
The following is how to install NVM using MacPorts:
sudo port install nvm
to install NVM through MacPorts. export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
source ~/.zshrc
. nvm --version
to verify the installation. If you see the version number, then you can start!
This is where NVM really comes into play. I remember working on two projects at the same time (this is my basic situation) – one requires Node.js 14 for stability and the other requires Node.js 18 for the latest features. Without NVM, switching the version would be a nightmare and requires uninstalling, reinstalling, and praying that there will be no problems or anything missing during the process. Once I discovered NVM, it felt like I had taken off the burden on my shoulders.
The following is how to make Node.js version management easy:
nvm install 14
and NVM will download and set it up. It's as simple as grabbing a cup of coffee from your favorite cafe – no drama. nvm use 16
and you're done. I use this feature frequently when testing updates in a sandbox environment. nvm alias default 14
to set Node.js 14 as your preferred version. This way, it will load automatically when you open the terminal – perfect for long-term projects. With NVM, you can quickly test, debug and run projects in different Node.js environments without any effort. I've saved countless hours (and avoided a lot of headaches) by letting NVM handle version management for me.
It is much easier to manage Node.js on macOS using NVM. Whether you install it using curl, Homebrew, or MacPorts, the process is very simple and flexible. After installation, switching between Node.js versions takes only a few seconds, avoiding compatibility issues.
To check if NVM is installed on your Mac, run nvm --version
in your terminal. If the version number is displayed, it means that NVM is installed.
Install NVM on Zsh:
curl -o- https://raw.githubusercontent.... | bash
. nano ~/.zshrc
. export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
source ~/.zshrc
. nvm --version
. To set up the path to NVM on your Mac, add export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
to your shell configuration file and run source ~/.zshrc
in your terminal.
Note that I cannot copy the command in the output in full since the full curl or wget command link is omitted from the original text. Please find the correct installation command link by yourself.
The above is the detailed content of How to install NVM on Mac for managing Node.js. For more information, please follow other related articles on the PHP Chinese website!