How to Install Node.js in MacOS

Developers can run JavaScript code on the server side with Node.js, an open-source, cross-platform runtime environment. It’s crucial to have Node.js installed on your macOS computer since more and more websites and applications use it for backend development.

We will go over how to install Node.js on macOS in this post, making sure you have everything you need to begin creating and executing Node.js apps.

Prerequisites

Make sure your macOS system satisfies the following prerequisites before we begin the installation process:

  1. Make sure you have macOS 11 or later installed.
  2. In order to install software on your Mac, you must have administrative rights.
  3. Basic familiarity with Zsh and the macOS terminal.

Methods to Install Node.js on macOS

Node.js macOS can be installed in a number of ways, such as by:

  1. Visiting the official Node.js website.
  2. Use the well-known macOS package manager Homebrew.
  3. Utilizing Node Version Manager (NVM) for installation.

Let’s examine each of the three approaches since they each have benefits.

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
node --version
node --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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
npm -v
npm -v
npm -v
npm version

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
npm install -g npm
npm install -g npm
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
/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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
brew update
brew update
brew update

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
brew install node
brew install node
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
source ~/.zshrc
source ~/.zshrc
source ~/.zshrc

Confirm NVM installation by running:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
nvm -v
nvm -v
nvm -v

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
nvm install --lts
nvm install --lts
nvm install --lts

Set the installed version as the default version:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
nvm alias default node
nvm alias default node
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
mkdir ~/.npm-global
mkdir ~/.npm-global
mkdir ~/.npm-global
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
npm config set prefix '~/.npm-global'
npm config set prefix '~/.npm-global'
npm config set prefix '~/.npm-global'

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
export PATH=~/.npm-global/bin:$PATH
export PATH=~/.npm-global/bin:$PATH
export PATH=~/.npm-global/bin:$PATH

Apply the changes by running the following:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
source ~/.zshrc
source ~/.zshrc
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?

Related Post

Node JS ubuntu

How to Install Node.js on Debian

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

How to Install XCode on MacOS

XCode is Apple's integrated development environment (ID...

How to Install Redis on MacOS

Redis is an in-memory data structure store used as a da...

node js docker

How to Build a Node.js App with Docker

Docker is known as the industry leader when it comes to...

How to Install GraphQL in Node JS

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

How to Install Multiple Versions of Node J...

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

Leave a Comment