Node.js Setup: MacOS

To download NodeJS for macOS, head over to https://nodejs.org/en/download/current/

At the time of writing this article, the current version of NodeJS is 7.2.0 and NPM is 3.10.9.

1195

Download and run the macOS Installer (.pkg)

732

By default NodeJS will install to /usr/local/bin/node and NPM will install to /usr/local/bin/npm

732

You can check that NodeJS and NPM are installed correctly by confirming their version numbers from Terminal by running node -v and npm -v.

617

You can test NodeJS by running it interactively. From Terminal enter node, then console.log("Helloworld!");

589

You will need to complete the next section if you wish to install npm packages globally.

Installing Node modules globally

You can install some npm packages globally, such as the droplit.io CLI, to run them from any directory.

npm install droplit-cli -g

589

However, you may encounter an error like this when installing npm packages globally.

617

There are a few ways to solve this issue according to docs.npmjs.com. We prefer Option 2 as it does not require changing directory ownership, which may be a security risk.

First, change the install location of global packages.

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

Finally, create/edit .bash_profile to add the new directory to your PATH.

echo "export PATH=~/.npm-global/bin:\$PATH" >> ~/.bash_profile

Restart your terminal and you should be able to install and use global packages.

📘

You use .bash_profile on macOS terminal, .bashrc for Xterm.

The .bash_profile is only executed for a login-shell, whereas .bashrc is executed for every new shell instance. Xterm follows this convention. However, Terminal.app on macOS, does not follow this convention. When Terminal.app opens a new window, it will run .bash_profile. A good article explaining this can be found here: http://scriptingosx.com/2017/04/about-bash_profile-and-bashrc-on-macos/