Installing Node.js on macOS

Successfully deploying a Node.js environment on macOS is the critical first step in modern web backend development. Unlike Linux or Windows, macOS comes with Node.js pre-installed by default; however, these versions are often outdated and unstable. This article details how to obtain the latest stable (LTS) version of Node.js and guides you through the entire process from download and installation to verification.
Before beginning the installation, it is essential to check your macOS version and the current state of installed Node.js. Open Terminal and run the following command to view your operating system version:

sw_vers

Next, inspect the currently active versions of Node.js and npm:

node -v
npm -v

If the version numbers are below 18.x or 20.x, it is strongly recommended that you upgrade. The default Node.js bundled with macOS typically only supports older LTS versions and cannot meet the requirements for current mainstream frameworks (such as React, Next.js, Vue, etc.).

Choosing an Installation Method: Homebrew vs nvm

On macOS, there are two primary mainstream methods to install and manage Node.js: Homebrew and nvm (Node Version Manager).

  • Homebrew: Suitable for most users; it installs Node.js into /usr/local/bin or /opt/homebrew/bin on Apple Silicon. The paths are relatively fixed.
  • nvm: Designed specifically for multi-version management, allowing you to switch between different Node.js versions across projects with high flexibility.

For beginners or single-environment development, Homebrew is the simplest and most direct choice; if you require frequent version switching, nvm is the superior solution. The following sections will focus on the installation steps using Homebrew.

Installing Node.js via Homebrew

1. Install Homebrew

First, you need to install the package manager Homebrew itself. Run the following command in Terminal:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

During installation, environment variable paths will be configured automatically. Upon completion, run source ~/.zshrc or source ~/.bash_profile to activate the configuration, then re-enter brew --version to confirm that Homebrew is ready.

2. Upgrade Node.js

Homebrew provides an automatic update mechanism; simply execute the following command to upgrade Node.js to the latest stable version:

brew upgrade node

This command will automatically download and install the newest LTS version compatible with your current macOS architecture (Intel or Apple Silicon).

3. Verify Installation Results

After installation, it is crucial to re-enter the version numbers in Terminal to confirm that the update was successful:

node -v
npm -v

The displayed versions should correspond to the latest release you expect. For example, if v20.11.0 and 10.2.4 are shown respectively, this indicates a successful installation.

Managing Multiple Versions with nvm (Advanced)

If you wish to flexibly switch between different Node.js versions in the future, it is recommended that you install nvm. The steps below provide a brief overview:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash
source ~/.zshrc
nvm install --lts
nvm use --lts

nvm install --lts will automatically install the latest Long Term Support (LTS) version, while nvm use --lts sets it as the default. The advantage of this method is that you can specify a particular version for project development at any time using commands like nvm install 18 or nvm install 20.

Common Issues and Precautions

The following issues may be encountered during installation:

  • Insufficient Permissions: If prompted with Permission denied, ensure that you have granted Homebrew administrative privileges by entering your admin password.
  • Architecture Mismatch: When installing on Apple Silicon (M1/M2) chips, Homebrew automatically detects the architecture and downloads the ARM64 version; manual specification is not required.
  • Path Conflicts: If both Homebrew and nvm are installed simultaneously, it is recommended to prioritize using nvm for Node.js management to avoid confusion between system default paths and user environment paths.

Summary

Through the steps outlined above, you have successfully deployed a latest version of the Node.js development environment on macOS. Whether through Homebrew's one-click upgrade or leveraging nvm's multi-version management capabilities, both approaches provide a solid foundation for your backend development. Remember to regularly update dependencies using npm update to keep your project running in optimal condition.