gyp WARN EACCES user "root" does not have permission to access the dev dir

Dmitri Zaitsev picture Dmitri Zaitsev · Apr 6, 2015 · Viewed 28.8k times · Source

Trying to

sudo npm install protractor -g

and the same notorious error/warning again (googled to no avail):

gyp WARN EACCES user "root" does not have permission to access the dev dir "/Users/dmitrizaitsev/.node-gyp/0.12.0"

What seems to happen is that node version 0.12.0 is downloaded and rebuilt, again and again during the same installation, despite of being the current node version on my machine:

node -v
v0.12.0

Questions:

  • The directory "/Users/dmitrizaitsev/.node-gyp/0.12.0" is actually missing! Why such a misleading message?

  • Why was this directory not created neither during the node v0.12.0 nor during the previous successful rebuild with node-gyp?

  • (Obviously) How can I prevent this from happening?

I run Mac OSX 10.8.5 if that is of any importance.

Answer

Dmitri Zaitsev picture Dmitri Zaitsev · Apr 22, 2015

UPDATE. There is a better way - changing npm's default global directory to user sub-directory to which you already have correct permissions, so no need to mess with system file's permissions or ownership in first place.

As recommended in https://docs.npmjs.com/getting-started/fixing-npm-permissions:

  1. Make a directory for global installations:
mkdir ~/.npm-global
  1. Configure npm to use the new directory path:
npm config set prefix '~/.npm-global'
  1. Open or create a ~/.profile (or ~/.bash_profile etc) file and add this line (at the end of the file):
export PATH=~/.npm-global/bin:$PATH
  1. On the command line, update your system variables:
source ~/.profile

or source ~/.bash_profile

See also Sindre Sorhus's guide on the topic: https://github.com/sindresorhus/guides/blob/master/npm-global-without-sudo.md


Have now figured what was wrong:

The directory had wrong permissions - it was not writable (which would have been a better error message than "accessible").

And because it was not writable, a temporary directory was used and deleted after every use, which is why the whole download had to run again and again.

The solution is to set user permissions with

sudo chown -R $USER <directory>

and never sudo npm again. It seems whenever you run sudo npm, all subdirectories created get wrong permissions, which will lead to problems later on.

See here for more details.