Fixing issues with npm permissions and EACCES errors

For as long as I can remember, when working with npm I have always received EACCES errors when trying to execute simple npm commands. This resulted in the need to prefix everything with sudo.

This wasn’t much of a nuisance when I was just getting used to working with npm. However, I got more comfortable with it, I got tired of having to type sudo every time. I finally went in search of an answer and it turns out it was a rather simple one. After reading through several threads on different sites and ended up using one of the methods described on the official npm site – imagine that.

The EACCES errors were the result of incorrect user permissions on my local npm directories. The answer was as simple as (1) changing the permission to npm’s default directory via terminal with two simple commands. Other options for fixing the issue were also available, such as; (2) changing npm’s default directory and (3) Installing npm with a package manager such as Homebrew. The first option seemed to be the most straight-forward and appropriate for my scenario so that’s what I tried first.

How to go about updating the permission to npm’s default directory

First, you should back up your work somehow. Next, locate your path to npm’s directory:

$ npm config get prefix

For most developers, as was the case for me, the path should be /usr/local .

WARNING: If the displayed path is just /usr, switch to Option (2) or you will mess up your permissions.

Change the owner of npm’s directories to the name of the current user (your username!):

sudo chown -R $(whoami)
$(npm config get prefix)/{lib/node_modules,bin,share}

This will update the permissions of the sub-folders used by npm and some other tools as well.