Installing and managing Node versions with n

October 26, 2015

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 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

Install n

We’ll install node via n: a Node version management tool. If you have Node installed already via brew, remove it, then:

$ npm i -g n

Install a version of node (ie the latest stable)

$ n stable

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 and n for now)

Enjoy! You should now be able to install and manage different node versions on your machine. Check the n documentation!

comments powered by Disqus