Installing Node and NPM Properly on a Dev Mac (via NVM)

If you've worked with npm, you've probably experienced permissions issues when installing packages globaly using 'npm install -g'. The issue lies with the ownership of certain folders (depending on how you installed node), most likely it will be ~/usr/lib/bin that is giving you trouble.

There are a couple ways around this. You could use the sudo prefix to run any global installs as the super user, or you could use 'sudo chown' to make your account the 'owner' of that folder. However effective, these solutions are not ideal. Using 'sudo' to solve your problems is a dangerous game. There may be unforseen consequences further down the road, which is especially dangerous in this case since you'll be messing with core system folders.

So how do we get around that annoying permissions issue, without using sudo?

Well if you're working on/setting up your own development machine, I highly recommend using Node Version Manager (NVM). Not only does NVM allow you to easily install Node without root permissions, you can also quickly change what version of node you're using. As projects and packages you work with might require different versions of Node to run, this is the perfect solution!

To install NVM, run the install.sh using the following curl command (found in the readme):

curl -o https://raw.githubusercontent.com/creationix/nvm/v0.25.4/install.sh | bash  

Onces that's done, you'll have to install node itself:

// For the latest stable release:
nvm install stable

// For a specific version, eg 0.10.38:
nvm install 0.10.38

// To switch between installed versions:
nvm use 0.10.38  

One problem with this setup is that NVM will 'forget' to use node when you open up a new terminal. It can get tedious to have to run 'nvm use 'version'' everytime. To get around this, you can set a default node version to be used with this:

// If you don't want to set the latest version
// replace stable with the desired version #
nvm alias default stable  

And there you have it! You've successfully installed Node through NVM, freeing you from all your permission woes!