Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Instructions on how to fix npm if you've installed Node through Homebrew on Mac OS X or Linuxbrew

READ THIS Very Important Update

This entire guide is based on an old version of Homebrew/Node and no longer applies. It was only ever intended to fix a specific error message which has since been fixed. I've kept it here for historical purposes, but it should no longer be used. Homebrew maintainers have fixed things and the options mentioned don't exist and won't work.

I still believe it is better to manually install npm separately since having a generic package manager maintain another package manager is a bad idea, but the instructions below don't explain how to do that.

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 correct defaults, configure the location for global npm modules to be installed, and then install npm as its own pacakge.

# OBSOLETE NOW! DO NOT USE ANY OF THIS! (See first paragraph of the gist)
rm -rf /usr/local/lib/node_modules
brew uninstall node
brew install node --without-npm
echo prefix=~/.npm-packages >> ~/.npmrc
curl -L https://www.npmjs.com/install.sh | sh

Node and npm should be correctly installed at this point. The final step is to add ~/.npm-packages/bin to your PATH so npm and global npm packages are usable. To do this, add the following line to your ~/.bash_profile:

export PATH="$HOME/.npm-packages/bin:$PATH"

Now you can re-install any global npm packages you need without any problems.

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. 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.

@sa-webb
Copy link

sa-webb commented Feb 3, 2020

For anyone using zsh instead of bash make sure you add export PATH="$HOME/.npm-packages/bin:$PATH" to your ~/.zshrc instead of ~/.bash_profile

@cotiga
Copy link

cotiga commented Feb 21, 2020

Following the update to Macos 10.13, when I use "npm run watch", "Node" remains blocked about 50% of processor usage (under 10.12 "Node" was disappearing from the activity monitor when "npm" terminated the rendering)

I did the described maneuver, it works, but "Node" still occupies 50%, even at rest :/

@FrederickEngelhardt
Copy link

Thanks a bunch. Testing a new environment out and ended up with this error.

@cotiga
Copy link

cotiga commented Feb 26, 2020

Following the update to Macos 10.13, when I use "npm run watch", "Node" remains blocked about 50% of processor usage (under 10.12 "Node" was disappearing from the activity monitor when "npm" terminated the rendering)

I did the described maneuver, it works, but "Node" still occupies 50%, even at rest :/

I solved my problem:
npm install fsevents

@kawan2019
Copy link

😍😘

@httpmurilo
Copy link

Ty.
Its works!

@ngnzone
Copy link

ngnzone commented Jun 19, 2020

Thanks so much
It saved me a great deal of time on ubuntu 18

@samuelfatoki
Copy link

Thanks a lot dude!

@sanjeevkse
Copy link

Thank you

@anandchakru
Copy link

brew uninstall node gave me the following error:

Error: Refusing to uninstall /usr/local/Cellar/node/15.0.1
because it is required by yarn, which is currently installed.
You can override this and force removal with:
  brew uninstall --ignore-dependencies node

brew ls yarn watchman node & brew ls npm helped me realize that.. and then removed them using brew uninstall yarn watchman node

@anthonybrown
Copy link

anthonybrown commented Mar 8, 2021 via email

@anthonybrown
Copy link

anthonybrown commented Mar 8, 2021 via email

@roxanemace
Copy link

Thank you!

@damnDasha
Copy link

BLESS YOUR HEART

@moutikabdessabour
Copy link

thank you

@ntbdy
Copy link

ntbdy commented Sep 17, 2021

I get stuck on the second line --

brew install node --without-npm

Error: invalid option: --without-npm

@BrianHHough
Copy link

This is a lifesaver, thank you so much!

@JMiddey
Copy link

JMiddey commented Dec 8, 2021

Hi I have tried to follow this code, the first line rm -rf /usr/local/lib/node_modules I I inputted and I get permission denied on all outputted lines. Any way around this?

@DanHerbert
Copy link
Author

This guide is obsolete, so getting errors is expected. If you came here looking for a solution to a specific problem you were getting, keep searching elsewhere since these commands will not work and are going to create more problems. (I mention this in the first paragraph of the gist but it's understandable if you skipped directly to the code block).

@JMiddey
Copy link

JMiddey commented Dec 8, 2021

oh dam that sucks haha, ive been stuck on the same issue all day and am yet to find a solution.. I guess the quest continues lol, thanks for the reply

@sa-webb
Copy link

sa-webb commented Dec 11, 2021

@JMiddey do not use Homebrew to install Node, use NVM and try to avoid installing any global packages instead use NPX. In fact, do not use homebrew for anything if you can get away with it, use Docker.

@JMiddey
Copy link

JMiddey commented Dec 11, 2021

Ive managed to sort my issues now, I think using a virtual environment was what saved me! How come installing glovally should be avoided?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment