Skip to content

Instantly share code, notes, and snippets.

@DanHerbert
Last active September 15, 2024 20:33
Show Gist options
  • Save DanHerbert/9520689 to your computer and use it in GitHub Desktop.
Save DanHerbert/9520689 to your computer and use it in GitHub Desktop.
Instructions on how to fix npm if you've installed Node through Homebrew on Mac OS X or Linuxbrew

OBSOLETE

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.

@mliradelc
Copy link

mliradelc commented Oct 18, 2019

rm -rf /usr/local/lib/node_modules
brew uninstall node
brew install node --without-npm

I got following error: any suggestions?
Error: invalid option: --without-npm

macOS hight Sierra
10.13.4

Error: invalid option: --without-npmThis formula was deprecated and only now works without it.

@frostini
Copy link

I believe @baotpham is right, this post very important but solution is quite outdated. Now days (October 2019) is much easier to use nvm. Plus you get a handy way to install the current LTS version and switching between these versions:

  1. Check installed versions of node and uninstall them first.
  2. Update your Homebrew and install nvm:
brew update
brew install nvm
  1. Follow the nvm instructions (create its folder and place the variables in your ~/ .bash_profile) and restart your terminal session.
  2. Install the desired node version: nvm install --lts or nvm install node.

You should be good to install npm global packages with no issues. To test, you can try to update npm itself with npm i -g npm, restart your terminal session, then check if version is updated.

thanks @tuliomarchetto, that got me going !

@ellew00ds
Copy link

Looks like --without-npm is no longer supported. I was able to make this work without that flag. Thanks Dan!

@othenos
Copy link

othenos commented Oct 24, 2019

I agree, seems to work without using --without-npm flag

@jess-dam
Copy link

Same here, thanks!

@fajarnugraha11
Copy link

Thanks a lot!

@ZakAnun
Copy link

ZakAnun commented Dec 7, 2019

I believe @baotpham is right, this post very important but solution is quite outdated. Now days (October 2019) is much easier to use nvm. Plus you get a handy way to install the current LTS version and switching between these versions:

  1. Check installed versions of node and uninstall them first.
  2. Update your Homebrew and install nvm:
brew update
brew install nvm
  1. Follow the nvm instructions (create its folder and place the variables in your ~/ .bash_profile) and restart your terminal session.
  2. Install the desired node version: nvm install --lts or nvm install node.

You should be good to install npm global packages with no issues. To test, you can try to update npm itself with npm i -g npm, restart your terminal session, then check if version is updated.

thanks @tuliomarchetto, that got me going !

This work for me, installed nvm via brew, we can use nvm to install node, after that npm upgrade normally. In addition, global install work also! thanks @frostini

@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?

@asakew
Copy link

asakew commented Sep 12, 2024

thanks you)

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