Skip to content

Instantly share code, notes, and snippets.

@SPorwal
Forked from DanHerbert/fix-homebrew-npm.md
Last active August 29, 2015 14:19
Show Gist options
  • Save SPorwal/d9d12310c6d2de5e330f to your computer and use it in GitHub Desktop.
Save SPorwal/d9d12310c6d2de5e330f to your computer and use it in GitHub Desktop.

Fixing npm On Mac OS X for Homebrew Users

Explanation of the issue

If you're a Mac Homebrew user and you installed node via Homebrew, there is a major philosophical issue with the way Homebrew and NPM work together. If you install node with Homebrew try to do npm update npm -g, you will see error like this:

$ 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  [email protected] { [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

There's an NPM bug open for this exact problem and a long thread that is still actively being debated, but the summary is that npm is its own package manager and it is therefore better to have npm manage itself and its packages instead of letting Homebrew do it. Also, using the Homebrew version of npm requires sudo to install global packages. That's also a very bad idea.

Solution

This solution fixes the error caused by trying to run npm update npm -g. Once you're finished, you also won't need to use sudo to install npm modules globally.

First, remove globally installed node modules. This step may not be necessary, but I do it to ensure my machine is clean before continuing. This step will be very specific to your setup. I've provided the one I ran as an example only. All other steps are completely copy/paste verbatim after this one.

sudo npm uninstall -g grunt-cli yo uglify-js nodemon jshint http-server bower

Next, run the following commands to remove node, re-install it with the right defaults, install npm as its own module, and configure the location for global npm modules to go.

brew uninstall node
brew install node --without-npm
echo prefix=~/.node >> ~/.npmrc
curl -L https://www.npmjs.org/install.sh | sh

Node and npm should be correctly installed at this point. The final step is to add ~/.node/bin to your PATH so commands you install globally are usable. I added this line to my ~/.path, which gets called via ~/.bash_profile. If you want to see a full example you can check out my dotfiles repo on github.

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

Now you should re-install any global node packages again. As an example, these are the ones I know I'll use:

npm install -g grunt-cli http-server uglify-js jshint bower yo nodemon
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment