Install Node.js on Mac

I encourage install Node.js using Node Version Manager (nvm) tool, so that you can switch easily between different versions of Node.js, specially when you are working with different projects that are tied to different versions of Node.js.

Install nvm

On Mac machines, you can use Homebrew to install nvm:

$ brew install nvm

If the installation goes successfully, you will see following comments in installation logs:

==> Renamed Formulae
 confluent-oss -> confluent-platform                          transmission -> transmission-cli
 ==> Downloading https://github.com/creationix/nvm/archive/v0.34.0.tar.gz
 ==> Downloading from https://codeload.github.com/nvm-sh/nvm/tar.gz/v0.34.0
 ################################################################## 100.0%
 ==> Caveats
 Please note that upstream has asked us to make explicit managing
 nvm via Homebrew is unsupported by them and you should check any
 problems against the standard nvm install method prior to reporting.
 You should create NVM's working directory if it doesn't exist:
 mkdir ~/.nvm
 Add the following to ~/.zshrc or your desired shell
 configuration file:
 export NVM_DIR="$HOME/.nvm"
   [ -s "/usr/local/opt/nvm/nvm.sh" ] && . "/usr/local/opt/nvm/nvm.sh"  # This loads nvm
   [ -s "/usr/local/opt/nvm/etc/bash_completion" ] && . "/usr/local/opt/nvm/etc/bash_completion"  # This loads nvm bash_completion

In “Please note …” phrase please notice that you need to create a folder called .nvm in your home director:

$  mkdir ~/.nvm

And based on your shell terminal manager you need to put following lines in their startup profile files:

export NVM_DIR="$HOME/.nvm"
[ -s "/usr/local/opt/nvm/nvm.sh" ] && . "/usr/local/opt/nvm/nvm.sh"  # This loads nvm
[ -s "/usr/local/opt/nvm/etc/bash_completion" ] && . "/usr/local/opt/nvm/etc/bash_completion"  # This loads nvm bash_completion

For Zsh you will put above lines in ~/.zshrc file and for bash in ~/.bash_profile

Install Node.js

Now for install node.js version 10.16.3 for example, you can use nvm command like this:

 $ nvm install 10.16.3

You should be able to see a log like this, you it is successfully installed:

Downloading and installing node v10.16.3…
Downloading https://nodejs.org/dist/v10.16.3/node-v10.16.3-darwin-x64.tar.xz…
################################################################## 100.0%
Computing checksum with shasum -a 256
Checksums matched!
Now using node v10.16.3 (npm v6.9.0)
Creating default alias: default -> 10.16.3 (-> v10.16.3)

As you see in the last line the default alias will be assigned to your first node.js installation

Using node –version, you can check your installed node version:

$ node --version
v10.16.3

As the part of node.js installation the Node Package Manager (npm) command tool also will be installed. You can check npm command line tool version using –version switch as well:

npm --version
6.9.0