152

I'm trying to install node-g.raphael, and I'm getting the following error:

Bender-03:htdocs alfred$ sudo npm install node-g.raphael --save
Password:

> [email protected] install 
  /Users/alfred/Sites/twistedgeo/htdocs/node_modules/contextify
> node-gyp rebuild

gyp ERR! configure error
gyp ERR! stack Error: EACCES: permission denied, mkdir 
  '/Users/alfred/Sites/twistedgeo/htdocs/node_modules/contextify/build'
gyp ERR! System Darwin 16.7.0
gyp ERR! command "/usr/local/bin/node" 
  "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" 
  "rebuild"
gyp ERR! cwd 
  /Users/alfred/Sites/twistedgeo/htdocs/node_modules/contextify
gyp ERR! node -v v8.8.1
gyp ERR! node-gyp -v v3.6.2
gyp ERR! not ok
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] 
  (node_modules/contextify):
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] 
  install: `node-gyp rebuild`
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: Exit status 1

+ [email protected]
added 2 packages and updated 1 package in 6.556s

I've tried this solution, but it doesn't solve my problem, still getting the same error.

Let me know if there's anything I can add to help in solving this.

6 Answers 6

471

Saw this from Fixing npm permissions and it helped, maybe you could give it a shot as well.

Option 1: Change the permission to npm's default directory

  1. Find the path to npm's directory:

    npm config get prefix

For many systems, this will be /usr/local.

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

  1. 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 changes the permissions of the sub-folders used by npm and some other tools (lib/node_modules, bin, and share).

Option 2: Change npm's default directory to another directory

There are times when you do not want to change ownership of the default directory that npm uses (i.e. /usr) as this could cause some problems, for example if you are sharing the system with other users.

Instead, you can configure npm to use a different directory altogether. In our case, this will be a hidden directory in our home folder.

  1. Make a directory for global installations:

    mkdir ~/.npm-global

  2. Configure npm to use the new directory path:

    npm config set prefix '~/.npm-global'

  3. Open or create a ~/.profile file and add this line:

    export PATH=~/.npm-global/bin:$PATH

  4. Back on the command line, update your system variables:

    source ~/.profile

Test: Download a package globally without using sudo.

`npm install node-g.raphael --save`

Instead of steps 2-4, you can use the corresponding ENV variable (e.g. if you don't want to modify ~/.profile):

NPM_CONFIG_PREFIX=~/.npm-global

Option 3: Use a package manager that takes care of this for you.

If you're doing a fresh install of Node on Mac OS, you can avoid this problem altogether by using the Homebrew package manager. Homebrew sets things up out of the box with the correct permissions.

brew install node

13
  • Thanks @Antz. I had to do the same thing to my local node_modules, probably because I had attempted installing node-g.raphael with sudo. d'Oh! Though I now have another issue related to xcode... But that'll be for another question. Commented Nov 12, 2017 at 19:52
  • 2
    This answer helped me big time. Thank you for the well crafted and understandable answer. one note, if you use the Fish shell and you are using option 1 then you'll need to remove the $ in the command so it will read as follows: sudo chown -R (whoami) (npm config get prefix)/{lib/node_modules,bin,share} again thanks for the great answer!
    – Chucky
    Commented Nov 15, 2019 at 6:56
  • 2
    I tried doing OPTION 2 steps. On MacOS Catalina v10 the default shell is ZSH which uses ~/.zprofile instead of ~/.profile. That is why with configuration above did not work at first on terminal but when used BASH shell it worked. You can find detailed description here: superuser.com/a/187673 Commented Mar 20, 2020 at 11:53
  • Does granting NPM all of the same permissions as your primary user account grant scripts executed by NPM permission to read/write to all directories the primary user can r/w? And make network requests? Since NPM scripts are rarely studied in full by downloading user, doesn't it make more sense from a security standpoint to create a separate user for NPM and grant r/w permission to that user only on the global NPM directories + local development directories? Commented May 30, 2020 at 6:12
  • 1
    I was trying to install node and nvm on macOS Montery and when trying to install a package (npm install -g ...) I kept getting a 'EACCES' error. Option one solved the permission issue for me.
    – ckoerner
    Commented Aug 2, 2022 at 14:53
11

I use:

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

Rather than changing your file permissions and risking confusion within your system (what I have done and may live to regret at a later date), may I suggest the following that I found after the fact.

Resolving EACCES permissions errors when installing packages globally

0
Brew uninstall node 
Brew install node 

create a link if there is an error and if link doesn't work do the following Simply run these four commands:

  sudo chown -R `whoami`:admin /usr/local/include/node
    sudo chown -R `whoami`:admin /usr/local/bin
    sudo chown -R `whoami`:admin /usr/local/share
    sudo chown -R `whoami`:admin /usr/local/lib/dtrace 

Finally, run the overwrite command:

brew link --overwrite node

-7

To get access on Mac/Linux, simply use this command

sudo npm install -g {package_name}

And you are good to go.

2
-16

Issue fixed for me by running the below command.

sudo npm install -g @angular/cli

Was able to install angular.

Then try running ng -v to confirm.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.