Properly Installing NodeJS via HomeBrew on OSX 10.11

If you are new to Homebrew and NodeJS or OSX itself ….

Firstly, there is nothing worse than tanking your dev environment when you have work to get done. A simple solution to that is to do yourself the favor of purchasing VMWare Fusion Pro and creating an OSX vm off of the recovery partition on your Mac.

While I am sure that seems a bit tedious ( and not exactly cheap )… How much is your time worth to you? I maintain a dev environment on a separate VM with a snapshot that matches the tooling running on my actual Mac. If you want to update something like NodeJS, Mongo, PHP and so on, it gives you a place to try it out before you tank your dev environment and learn of an issue the hard way.

Installing Homebrew & Versions

If you don’t already have Brew installed, you are going to need it to go any further with this guide ( not to mention it’s simply full of win ). We are going to install another package with this “Versions” which allows us to list all the nodejs versions available for install.

At the terminal:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew tap homebrew/versions

Already tried to install node via Brew?

If you have Node installed already via brew and then run smack into the Global package / sudo headache, we are going to remove that and install it properly.

**Warning**
The commands below are removing files and doing so with the -f (force) option. Typo === Tears :  I personally never copy and paste commands from the web. Type the commands below using tab to autocomplete the paths, be sure of what you are removing or regret it later.

At the terminal:

sudo rm -rf /usr/local/lib/node_modules
sudo rm -rf ~/.npm
brew uninstall node

Install NodeJS & NPM

Install NodeJS

The first thing you are going to want to do is figure out what version of Node you want which is where Homebrew/Versions comes into play.

At the terminal:

brew search node

This will give you output some like you see below. For the purposes of this guide, I will be installing “node4-lts”. If you want a older version choose one that is listed or if you want the latest choose “node” ( 5.1.0 at the time I am writing this ).

Install Node and NPM

Install node ( replace the < > with the node version of your choosing )

brew install <nodeVersionFromAbove> --without-npm

Create a directory for global packages in a location you have the proper permissions ( Your user folder is the best option )

mkdir "${HOME}/.npm-packages"

Add references to the directory you created above in your .bashrc & .npmrc files so the soon to be installed npm knows where to find it.

echo NPM_PACKAGES="${HOME}/.npm-packages" >> ${HOME}/.bashrc

echo prefix=${HOME}/.npm-packages >> ${HOME}/.npmrc

Install Node Package Manager

curl -L https://www.npmjs.org/install.sh | sh

Update your .bashrc to allow NodeJS and OSX to find your custom packages folder

echo NODE_PATH=\"\$NPM_PACKAGES/lib/node_modules\:\$NODE_PATH\" >> ${HOME}/.bashrc

echo PATH=\"\$NPM_PACKAGES/bin\:\$PATH\" >> ${HOME}/.bashrc

Update your .bash_profile so it always sources your .bashrc on login

echo source "~/.bashrc" >> ${HOME}/.bash_profile

Source your .bashrc to pick up the changes without having to log out

source ~/.bashrc

Validate your install

  • node -v : Should return the version of node you installed
  • npm -v : Should return whatever the lastest version of npm is for your node version.
  • npm list -g –depth=0 : Returns a list of installed node packages

 

With that you should be good to go. You can now install npm packages globally without having to contend with “sudo hell” or any other annoying permissions issues.

Happy Hacking 🙂