With the initial npm 5 release a few months ago, I immediately noticed a few issues with my local dev-setup. I’m a Homebrew user and I install node.js without npm. With the latest release of node: 8.4.0 and npm: 5.4.0, installing node through Homebrew can cause problems for globally installed packages. To properly fix this, use the steps below.

Note: I did contact npm support through e-mail in late May, 2017, their response time was amazing. However, their support was disappointing! Due to not finding a fix I was forced to use yarn for two months; ha.

The following steps will fix the error:

Before we begin, make a note of any globally installed npm packages. This guide will have you remove all of those packages. At the end of the guide you’ll need to re-install them. Also, once you’re finished, you won’t need to use sudo to install npm modules globally.

Run the following commands to remove all existing global npm packages, uninstall node and npm, re-install node with the right permissions, install npm as its own package, and configure the location npm modules to be installed.

1. Re-install node without npm

Just for fun, run:

Output should be:


2. Setup npm new home: /user/local/npm_packages

For bash users: add/append the following lines in ~/.bashrc

Make sure you load the .bashrc in your .bash_profile

  • IMPORTANT: Please make sure you don’t 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 line exists in your .bash_profile:
  • make sure .bashrc is executable

At this point go ahead and restart iTerm/Terminal

Please note: It is wise to close all existing iTerm/Terminal sessions and re-open iTerm/Terminal.

Advanced users; ha. Reload the .bash_profile:

For zsh users: add/append the following lines in ~/.zshrc

You can either close iTerm/Terminal or reload .zshrc:

3. Install npm directly from npmjs.com

Let’s go ahead and set the correct home in ~/.npmrc

Note: If the ~/.npmrc file does not exist, create one.

Open ~/.npmrc with your favorite text editor and make sure it has the following line:

Now, let’s run the npm install script from npmjs.com

Note: curl -L https://www.npmjs.com/install.sh | sh silently fails and doesn’t copy the npm package contents correctly. In order to fix this, let’s do the falling after running the npm install.sh script.

You may receive a failed message, do not worry let’s fix it:

That’s it. You should now be able to run the npm command without sudo.

A huge shoutout to Radu Cugut for providing this fix, go follow him already @rcugut

Happy coding!