# Fixing npm On Mac OS X for Homebrew Users Installing node through Homebrew can cause problems with npm for globally installed packages. To fix it quickly, use the solution below. An explanation is also included at the end of this document. ## 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. Before you start, make a note of any globally installed npm packages. These instructions will have you remove all of those packages. After you're finished you'll need to re-install them. Run the following commands to remove all existing global npm modules, uninstall node & npm, re-install node with the right defaults, install npm as its own pacakge, and configure the location for global npm modules to be installed. ### 1. Re-install node without npm ```bash # run the following commands # this next line erases all the existing global npm packages rm -rf /usr/local/lib/node_modules # reinstall node via brew, but without npm brew uninstall node brew update # always good to have the latest brew install node --without-npm ``` You should now be able to run the `node` command ### 2. Prepare the npm new home: `/usr/local/npm_packages` ```bash # run the following commands sudo mkdir -p /usr/local/npm_packages # normally, with Homebrew, your user should own the entire `/usr/local` folder, but this is just to be safe: # make sure your user is allowed to write into the folder sudo chown -R /usr/local/npm_packages ``` #### Add/append the following lines in `~/.bashrc` ``` # this is where all globally installed node packages will go export NPM_PACKAGES="/usr/local/npm_packages" export NODE_PATH="$NPM_PACKAGES/lib/node_modules:$NODE_PATH" # add to PATH export PATH="$NPM_PACKAGES/bin:$PATH" ``` #### And make sure you load the `.bashrc` in your `.bash_profile`: - **IMPORTANT:** make sure you do not have both a `.profile` and a `.bash_profile`; if you do, copy everything from `.profile` into `.bash_profile` and then remove the `.profile` - Check if the following or similar line exists in your `.bash_profile`: ``` [[ -s ~/.bashrc ]] && source ~/.bashrc ``` - make sure `.bashrc` is executable ```bash # run the following commands chmod +x ~/.bashrc ``` ### 3. Restart your iTerm / Terminal **IMPORTANT:** close all existing iTerm/Terminal sessions and re-open iTerm/Terminal before going further Advanced: or just reload the `.bash_profile` -- `source ~/.bash_profile` ### 3. Install npm from npmjs.com #### First, set the correct npm home in `~/.npmrc` Make sure `~/.npmrc` file exists, and contains a line as below: ``` prefix=/usr/local/npm_packages ``` #### Run the npm install script from npmjs.com ```bash curl -L https://www.npmjs.com/install.sh | sh ``` That's it. You should now be able to run the `npm` command. # Extra notes ------------ **NEVER** run `npm` as sudo; so never do `sudo npm install ...` ------------ ---------- ## Explanation of the issue If you're a 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 and then try to do `npm update npm -g`, you may see an 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 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 ``` There's an [NPM bug for this exact problem](https://github.com/npm/npm/issues/3794). The bug has been "fixed" by Homebrew installing npm in a way that allows it to manage itself once the install is complete. However, this is error-prone and still seems to cause problems for some people. The root of the the issue is really that `npm` is its own package manager and it is therefore better to have `npm` manage itself and its packages completely on its own instead of letting Homebrew do it. Also, using the Homebrew installation of `npm` will require you to use `sudo` when installing global packages. Since one of the core ideas behind Homebrew is that apps can be installed without giving them root access, this is a bad idea.