Update Node.js to the latest version on MacOS

In this article, I will show you how you can update Node.js to the latest version on Mac OS.

Node.js frequently releases new versions with updated features and improvements. Keeping your development environment up-to-date is crucial. Here are several methods to update Node.js on macOS without constantly downloading new installers.

Using Nvm (Node Version Manager)

The Nvm is a version manager for Node.js. It allows you to install multiple versions of Node.js and switch between them. This is particularly useful if you are working on different projects that require different Node.js versions.

Installation

You install Nvm using a curl command that fetches an install script and executes it with bash. It’s also available via Homebrew.

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
# or install via Homebrew
brew install nvm

Update Node.js

Once Nvm is installed, you can install the latest version of Node.js using the command mentioned below.

nvm install node --reinstall-packages-from=node

The --reinstall-packages-from=node option ensures that global npm packages from your previous Node.js version are reinstalled to the new version, keeping your environment consistent.

Using N (npm-based Node Version Manager)

N is another version manager for Node.js, but it is simpler and more straightforward than Nvm. It’s an npm package that you can use to manage Node.js versions.

Installation

First, you need to have Node.js installed. Then, you install N globally using npm.

npm install -g n

Update Node.js

With N installed, you can upgrade to the latest version of Node.js by running the below command.

sudo n latest

This command fetches and installs the latest Node.js version.

Using Homebrew

Homebrew is a popular package manager for macOS. It simplifies the installation of software on macOS.

Installation

If you haven’t installed Homebrew, run the below command in Terminal.

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

Update Homebrew

Run the below command to update Homebrew itself and get the latest package versions.

brew update

Install or update Node.js

Now, execute the below command to update Node.js using Homebrew.

brew install node

The above command will update Node.js to the latest version available in Homebrew.

If Node.js was previously installed with Homebrew just run the below command.

brew update
brew upgrade node

The brew update command updates the list of available packages and versions in Homebrew, while brew upgrade node upgrades Node.js to the latest version available in Homebrew.

Using MacPorts

MacPorts is another package manager for macOS, similar to Homebrew but with some differences in available packages and how they are managed.

Update MacPorts

Run the below command in the terminal to update MacPorts.

sudo port selfupdate

Update Node.js

Then, install the latest version of Node.js using the below command.

sudo port install nodejs-devel

The nodejs-devel port usually contains the more recent version of Node.js. You can also use nodejs instead of nodejs-devel if you prefer the long-term support (LTS) version.

Each method has its advantages, and the choice largely depends on whether you need to manage multiple Node.js versions (in which case Nvm or N would be more appropriate) or just want to keep Node.js up to date (where Homebrew or MacPorts would be simpler solutions).

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top