Skip to content

Instantly share code, notes, and snippets.

@zmilonas
Forked from DanHerbert/fix-homebrew-npm.md
Last active June 21, 2018 18:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zmilonas/474a9507e89268caab6fbc95fbd43b98 to your computer and use it in GitHub Desktop.
Save zmilonas/474a9507e89268caab6fbc95fbd43b98 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

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.

This is updated for 2018

export NPM_VERSION=6.1.0
rm -rf /usr/local/lib/node_modules
brew uninstall node
brew install node --without-npm
echo prefix=~/.npm-packages >> ~/.npmrc
curl -O https://registry.npmjs.org/npm/-/npm-${NPM_VERSION}.tgz
tar xzf npm-${NPM_VERSION}.tgz
cd package
node bin/npm-cli.js install -gf --prefix=~/.npm-packages ../npm-${NPM_VERSION}.tgz
cd ..
rm -rf package npm-${NPM_VERSION}.tgz

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.

See the original gist

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