On Mac OSX, Homebrew is a very good tool to help install and mange CLI tools, like Git, nodeJS and NPM.

However, if you installed NodeJS and NOM through the nomral command brew install node, you may see the following error when using npm update npm -g to update the NPM.

$ npm update npm -g
npm http GET https://registry.npmjs.org/npm
npm http 304 https://registry.npmjs.org/npm
npm http GET https://registry.npmjs.org/npm/1.4.4
npm http 304 https://registry.npmjs.org/npm/1.4.4
npm ERR! error rolling back Error: Refusing to delete: /usr/local/bin/npm not in /usr/local/lib/node_modules/npm
npm ERR! error rolling back     at clobberFail (/usr/local/Cellar/node/0.10.26/lib/node_modules/npm/lib/utils/gently-rm.js:57:12)
npm ERR! error rolling back     at next (/usr/local/Cellar/node/0.10.26/lib/node_modules/npm/lib/utils/gently-rm.js:43:14)
npm ERR! error rolling back     at /usr/local/Cellar/node/0.10.26/lib/node_modules/npm/lib/utils/gently-rm.js:52:12
npm ERR! error rolling back     at Object.oncomplete (fs.js:107:15)
npm ERR! error rolling back  npm@1.4.4 { [Error: Refusing to delete: /usr/local/bin/npm not in /usr/local/lib/node_modules/npm] code: 'EEXIST', path: '/usr/local/bin/npm' }
npm ERR! Refusing to delete: /usr/local/bin/npm not in /usr/local/lib/node_modules/npm
File exists: /usr/local/bin/npm
Move it away, and try again. 

npm ERR! System Darwin 13.1.0
npm ERR! command "/usr/local/Cellar/node/0.10.26/bin/node" "/usr/local/bin/npm" "update" "npm" "-g"
npm ERR! cwd /Users/dan/Google Drive/Projects/dotfiles
npm ERR! node -v v0.10.26
npm ERR! npm -v 1.4.3
npm ERR! path /usr/local/bin/npm
npm ERR! code EEXIST
npm ERR! 
npm ERR! Additional logging details can be found in:
npm ERR!     /Users/dan/Google Drive/Projects/dotfiles/npm-debug.log
npm ERR! not ok code 0

The error info is from the post – Fixing npm On Mac OS X for Homebrew Users.

To fix it, this post also provides a very good solution based on my test.

rm -rf /usr/local/lib/node_modules
brew uninstall node
brew install node --without-npm  # Be patient~ This step will do `make` the package. 
echo prefix=~/.node >> ~/.npmrc
curl -L https://www.npmjs.com/install.sh | sh

By dong this, the folder of NodeJS modules will be moved from /usr/local/lib/node_modules to user’s local folder ~/.node/lib. So that it can void the need of root access in some cases.

The final step is to add ~/.node/bin to your PATH (usually under the file ~/.bash_profile).

export PATH="$HOME/.node/bin:$PATH"

Don’t forget run source ~/.bash_profile to reload the config.

During this process, you may encounter some issues. Here are the some that I experienced during my test.

“brew” Link Issue

When doing brew install node --without-npm, you may have the following error:

Could not symlink share/doc/node/gdbinit
Target /usr/local/share/doc/node/gdbinit
already exists. You may want to remove it:
  rm '/usr/local/share/doc/node/gdbinit'

This usually indicates that your system has some legacy files that conflict the brew link. Run brew link -n node to list and delete them manually. Once done it, run brew link node to link.

“/usr/local” Permission Issue

If you have done some installation by using sudo (like sudo npm install -g XXX), you may encounter some weirdissue during your setup.

Try the following command to fix the permisison and then run brew doctor to check the status.

sudo chown -R $USER /usr/local
  1. One more thing.

    If you change the default location of node_modules folder, you need to specify the new location in NODE_PATH.

    # Set NODE_PATH for NodeJS
    export NODE_PATH="$HOME/.node/lib/node_modules:$NODE_PATH"

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>