53

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.

Dmitri Zaitsev
  • 13,548
  • 11
  • 76
  • 110

3 Answers3

90

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.

Dmitri Zaitsev
  • 13,548
  • 11
  • 76
  • 110
27

Try with:

sudo npm install -g module --unsafe-perm

Miki Nacucchi
  • 381
  • 2
  • 4
  • 3
    No, you don't `sudo npm` -- see my answer! – Dmitri Zaitsev Apr 22 '15 at 04:00
  • 1
    Thanks, it solved my problem. I could not implement the answer above, for some reason. – Damjan Pavlica Oct 05 '15 at 15:08
  • for me this worked. ran npm on root so needed to make sure it it could run as root. --unsafe-perm. Thanks for the fix – A H Bensiali Nov 03 '17 at 08:06
  • Also mentioned at pkg doc: https://www.npmjs.com/package/serialport/v/7.0.2 `if you're going to use sudo or root to install Node-Serialport, npm will require you to use the unsafe parameters flag. sudo npm install serialport --unsafe-perm --build-from-source` – Efren Jun 12 '20 at 02:52
2

That's because you do not have a folder in this directory "/Users/dmitrizaitsev/.node-gyp/0.12.0".

Just create a new folder named 0.12.0 which is the version number of your node

It will fix the problem.

Bob
  • 5,510
  • 9
  • 48
  • 80
Jack Lee
  • 929
  • 7
  • 5
  • 2
    2 years later, encountering this problem with node 9.0.0 and of course I don't have a directory `/usr/local/js/node-v9.0.0/lib/node_modules/@angular/cli/node_modules/node-sass/.node-gyp/9.0.0` because that's what the install script is supposed to be handling. That's what it is for after all. Instead, I get a "does not have permission" error and an infinite loop during install. The feel of the 90s. Clownish. – David Tonhofer Nov 01 '17 at 16:12
  • Well, the `mkdir` broke the installer out of its loop. On to the next problem. – David Tonhofer Nov 01 '17 at 16:21