Installing npm packages global on OSX

October 4, 2015

Quick guide to install node and npm globally and avoid using sudo to install npm packages.

We’ll be using the Z shell (zsh) instead of bash. Replace any reference to .zshrc with .bash_profile/.bashrc in case you are running on bash instead.


Install Node

We’ll install node via homebrew. If you have Node installed already via brew, remove it, then:

$ brew install node --without-npm

Install NPM

Create a directory for your global packages.

$ mkdir .npm-packages

Add a reference to the directory into zshrc

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

Tell NPM where to install global packages (in our case ~/.npm-packages)

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

Install the latest version of NPM

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

Add the following lines to your .zshrc to be ensure that node will find the packages and that you’ll find the installed binaries:

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

To verify if everything installed successfully just run the following commands:

$ node -v 
// check version of node installed

$ npm -v
// check version of npm installed

$ npm list -g --depth=0
// check global npm packages installed (should just be npm for now)

Enjoy! You should now be able to install npm modules globally without having to use sudo!

comments powered by Disqus