Installing Node.js

Install Homebrew

Homebrew is an excellent package manager for MacOS-specific programs. It essentially gives you a way to install certain programs (usually developer-y ones) directly from the command line. It's similar to how NPM or Bower works.

To install Homebrew:

  • Head to the homebrew home page
  • Follow the instructions by copying/pasting the bash command into your terminal (from any directory, doesn't matter where).

Install NVM

NVM is the Node Version Manager, which makes installing and switching between multiple release versions of Node.js really easy. We'll use the freshly-installed Homebrew to install nvm:

From terminal (from any directory):

brew install nvm

Once that completes, follow the instructions that print to the screen:

  • Run mkdir ~/.nvm (again, from anywhere in terminal)
  • Use a text editor (Vi or Nano would work well) to open (or create, if it doesn't exist yet) a file called .bash_profile inside your home directory:
vi ~/.bash_profile

Add the 2 lines to the bottom of the .bash_profile file:

export NVM_DIR="$HOME/.nvm"
. "/usr/local/opt/nvm/nvm.sh"

If you used Vi to add this, you can save and quit the file easily by hitting the "escape" key to exit out of insert mode, then hold "shift" and press "Z" twice to save and quit.

Run source ~/.bash_profile to re-run all the commands in that file, and now you should be able to run nvm --version and see a version number pop up. Congrats!


Use NVM to install Node.js and the Node Package Manager (NPM)

In your terminal, enter:

nvm install --lts
# or whatever version you found from the 
# site in the "Recommended For Most Users" green box.

Once that completes, you should be able to type node --version and npm --version and have version numbers pop up.

You're done! Enjoy JavaScript from the command line :)

Updating to new versions of node

NVM makes updating your node version very easy. If a newer version (let's say 8.12.0) is release, you can simply type nvm install 8.12.0 (or whatever the version number is) and you'll be updated to a newer version of node. The old versions are maintained on your machine, and can be switched to easily by entering nvm use 8.11.1, which will switch you back to that specific past version. This is useful if you have a past project that is perhaps broken on newer versions of node and you haven't yet had a chance to update the code to reflect changes that need to be made.

Updating to a new version of node will not bring over any global installations you had on the past version. To bring those over, simply run nvm copy-packages <old version of node here>. E.g. if you just updated from 8.11.1 to 8.12.0, you would run nvm copy-packages 8.11.1 after switching to a newer version of node.