node / npm failed to install via brew on OSX 10.9.4

I think this is a new issue & took me couple hours to figure it out:

$ brew install node -v
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/node-  0.10.31.mavericks.bottle.tar.gz

..

npm ERR! Error: EACCES, mkdir '/../.npm/nopt/2.1.2'
npm ERR!  { [Error: EACCES, mkdir '/../.npm/nopt/2.1.2']
npm ERR!   errno: 3,
npm ERR!   code: 'EACCES',

Issue of installing npm was solved by (thanks to https://github.com/Homebrew/homebrew/issues/28501#issuecomment-53907840):

$ brew update
$ sudo chown -R $USER /usr/local

but this messed up my system. I was not able to use 'sudo' any more till I run Repair Disk Permissions in Disk Utilities.

changing ownership of those system libraries to your end user account is NOT safe - additionally web servers should NEVER be owned by root for known security reasons and so goes for node - you have rendered your box open to evil

Here is a safe way to install node/npm on OSX/linux

to start fresh remove prior node.js and npm installs as well as these :

sudo mv ~/.npmrc ~/.npmrc_ignore
sudo mv ~/.npm   ~/.npm_ignore
sudo mv ~/tmp    ~/tmp_ignore
sudo mv ~/.npm-init.js ~/.npm-init.js_ignore

to install nodejs and npm as yourself NOT root do these commands (linux) :

mkdir ${HOME}/bin

download source from : http://nodejs.org/download/

cd node-v0.10.31

./configure   --prefix=${HOME}/bin/nodejs

make -j8
make install

which puts it into dir defined by above --prefix

export PATH=${HOME}/bin/nodejs/bin:$PATH

NODE_PATH so node can find dir for modules otherwise npm install xxx will put newly installed module into dir in curr dir :

export NODE_PATH=${HOME}/bin/nodejs/lib/node_modules

do above AND use syntax : npm install xxxxx -g always use the -g for global

nodejs install gives you npm as well :

ls -la ${HOME}/bin/nodejs/bin