How to Install Node.js in MacOS

Node.js is an open-source, cross-platform runtime environment that enables developers to execute JavaScript code on the server side. As more applications and websites use Node.js for backend development having it installed on your macOS machine is an important aspect.

In this article, we will cover the steps required to install Node.js on macOS ensuring you have the proper setup to start building and running Node.js applications.

Prerequisites

Before we get into the installation process ensure your macOS system meets the following requirements:

  1. Ensure you are running macOS 11 or more.
  2. You need administrative privileges to install software on your Mac.
  3. Basic knowledge of macOS terminal including Zsh.

Methods to Install Node.js on macOS

There are several methods to install Node.js macOS, including:

  1. Installing via the official Node.js website.
  2. Using Homebrew, a popular macOS package manager.
  3. Installing using Node Version Manager (NVM).

Each method has its advantages so let’s explore all three.

Method 1: Install via Official Node.js Website

This method involves downloading and installing the Node.js installer directly from the official website.

Open the Safari web browser and navigate to the Node.js official website. On the homepage, you will see two versions of Node.js: LTS (Long-Term Support) and Current. For most users, the LTS version is recommended as it provides stability and long-term support.

Click the download link for the LTS version to download Node.js macOS.

node js installer

Once the download is complete, open the downloaded .pkg file.

node js pkg file

The installer will launch. The installer will prompt you to agree to the license agreement and choose an installation location. By default, Node.js will be installed in /usr/local.

node js macos path

After installation, open the Terminal app. Run the following command to verify that Node.js is installed and check its version:

node --version

You should see the version number of Node.js displayed, indicating a successful installation.

node version

Node.js comes with npm by default. To check the installed version of npm, run:

npm -v
npm version

If npm is not installed, you can install it using the following command:

npm install -g npm

Method 2: Installing via Homebrew

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

Open Terminal. Install Homebrew by running the following command:

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

After installing Homebrew, update it to ensure you have the latest version:

brew update

Use Homebrew to install Node.js by running the following command:

brew install node

Homebrew will download and install Node.js along with npm. Run the node and npm version command to confirm that Node.js is installed.

Method 3: Install via Node Version Manager

NVM allows you to manage and install multiple versions of Node.js on a single machine, making it easy to switch between versions.

Open Terminal. Install NVM by running the following command:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

Close and reopen the Terminal, or source your profile using:

source ~/.zshrc

Confirm NVM installation by running:

nvm -v

Use NVM to install the latest LTS version of Node.js:

nvm install --lts

Set the installed version as the default version:

nvm alias default node

Run the node and npm version command to confirm that Node.js is installed.

Configuring Node.js

After installing Node.js, you can take a few additional steps to configure your environment and streamline your development workflow.

By default, npm installs global packages in a system directory that requires root privileges. To avoid using sudo it for every global package installation, create a new directory for global packages. After installing Node.js, you can take a few additional steps to configure your environment and streamline your development workflow.

Run the following commands in Terminal:

mkdir ~/.npm-global
npm config set prefix '~/.npm-global'

Add the following line to your shell profile ~/.zshrc:

export PATH=~/.npm-global/bin:$PATH

Apply the changes by running the following:

source ~/.zshrc

There are several npm packages you might find useful for development. Here are a few recommendations:

  • nodemon: Automatically restarts your Node.js application when file changes are detected.
  • express-generator: Scaffolding tool for quickly setting up an Express.js application.

Conclusion

Node.js is a powerful tool for server-side development and having it installed on your macOS machine opens up possibilities for building dynamic applications. This comprehensive guide has provided step-by-step instructions for installing Node.js using three different methods via the official website, Homebrew, and NVM. Additionally, we covered configuration tips to streamline your project workflow.

Install Node.js efficiently with Ultahost Mac VPS hosting which is one of the quickest, most secure, and most affordable Macintosh hosting. Connect to a MacOS VPS from any device. Start your Mac hosting today!

FAQ

What is Node.js?
How do I install Node.js on macOS?
What is Homebrew?
How do I check if Node.js is installed on my Mac?
Can I install multiple Node.js versions on macOS?
Is Node.js free to use?
Do I need administrative rights to install Node.js?

Related Post

How to Install Multiple Versions of Node J

Node.js is a powerful JavaScript runtime environment th...

How to Install TypeScript in Node JS

TypeScript is a strong, statically typed superset of Ja...

How to Install Node.js and NPM on Windows

Node.js enables the execution of JavaScript code outsid...

How to Install GraphQL in Node JS

GraphQL is a tool for APIs that lets clients request on...

How to Install Node-RED on Windows

Node-RED is a powerful flow based programming tool that...

How to Setup Next JS in Node JS

Next.js is a powerful React framework that enables deve...

Leave a Comment