npm throws error without sudo

2018-12-31 02:35发布

I just installed node and npm through the package on nodejs.org and whenever I try to search or install something with npm it throws the following error, unless I sudo the command. I have a feeling this is a permissions issue? I am already the admin.

npm ERR! Error: EACCES, open '/Users/chietala/.npm/-/all/.cache.json'
npm ERR!  { [Error: EACCES, open '/Users/chietala/.npm/-/all/.cache.json']
npm ERR!   errno: 3,
npm ERR!   code: 'EACCES',
npm ERR!   path: '/Users/chietala/.npm/-/all/.cache.json' }
npm ERR! 
npm ERR! Please try running this command again as root/Administrator.

npm ERR! System Darwin 12.2.0
npm ERR! command "node" "/usr/local/bin/npm" "search" "bower"
npm ERR! cwd /Users/chietala
npm ERR! node -v v0.10.4
npm ERR! npm -v 1.2.18
npm ERR! path /Users/chietala/.npm/-/all/.cache.json
npm ERR! code EACCES
npm ERR! errno 3
npm ERR! stack Error: EACCES, open '/Users/chietala/.npm/-/all/.cache.json'
npm ERR! 
npm ERR! Additional logging details can be found in:
npm ERR!     /Users/chietala/npm-debug.log
npm ERR! not ok code 0

30条回答
零度萤火
2楼-- · 2018-12-31 03:14

What to me seems like the best option is the one suggested in the npm documentation, which is to first check where global node_modules are installed by default by running npm config get prefix. If you get, like I do on Trusty, /usr, you might want to change it to a folder that you can safely own without messing things up the way I did.

To do that, choose or create a new folder in your system. You may want to have it in your home directory or, like me, under /usr/local for consistency because I'm also a Mac user (I prefer not to need to look into different places depending on the machine I happen to be in front of). Another good reason to do that is the fact that the /usr/local folder is probably already in your PATH (unless you like to mess around with your PATH) but chances are your newly-created folder isn't and you'd need to add it to the PATH yourself on your .bash-profile or .bashrc file.

Long story short, I changed the default location of the global modules with npm config set prefix '/usr/local', created the folder /usr/local/lib/node_modules (it will be used by npm) and changed permissions for the folders used by npm with the command:

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

Now you can globally install any module safely. Hope this helps!

查看更多
皆成旧梦
3楼-- · 2018-12-31 03:14

you could try this

sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}
查看更多
皆成旧梦
4楼-- · 2018-12-31 03:15

On Mac OS X, when installing with Homebrew's brew install npm, the installation path is /usr/local/share/npm/ with both bin/ and lib/node_modules/ subfolders.

Running this command to change to owner to your currently logged in user should fix it all up, and allow you to install global NPM packages without sudo.

sudo chown -R $USER ~/.npm /usr/local/share/npm/

查看更多
余欢
5楼-- · 2018-12-31 03:16

I encountered this when installing Recess (https://github.com/twitter/recess) to compile my CSS for Bootstrap 3.

When installing recess:

-npm install recess -g
  1. You need to unlock permissions in your home directory, like Noah says:

    sudo chown -R `whoami` ~/.npm
    
  2. You also need write permissions to the node_modules directory, like Xilo says, so if it still isn't working, try:

    sudo chown -R `whoami` /usr/local/lib/node_modules
    
  3. If you are still seeing errors, you may also need to correct /usr/local permissions:

    sudo chown -R `whoami` /usr/local
    

Please note that as indicated in this post /usr/local/ isn't actually a system dir if you are on a Mac, so, this answer is actually perfectly "safe" for Mac users. However, if you are on Linux, see Christopher Will's answer below for a multi-user friendly, system dir safe (but more complex) solution.

查看更多
刘海飞了
6楼-- · 2018-12-31 03:16

TL;DR

always use sudo -i or sudo -H when running npm install to install global packages.

--

When you use npm it downloads packages to your user home directory. When you run as sudo, npm installs files to the same directory, but now they are owned by root.

So this is what happens to absolutely every single person who has ever used npm:

  • install some local packages without issue using npm install foo
  • install global package using sudo install -g foo-cli without issue
  • attempt to install local package with npm install bar
  • get frustrated at the npm designers now that you have to go chmod a directory again

When you use the -i or -H option with sudo, your home directory will be root's home directory. Any global installs will cache packages to /root/.npm instead of root-owned files at /home/me/.npm.

Just always use sudo -i or sudo -H when running npm install to install global packages and your npm permissions problems will melt away.

For good.

http://hood.ie/blog/why-you-shouldnt-use-sudo-with-npm.html

-- q.v. the accepted answer for fixing an already fucked npm.

查看更多
孤独总比滥情好
7楼-- · 2018-12-31 03:17

This is how I solved the issue on Windows 8.1:

  • Go to your nodejs install (usually C:\Program Files\nodejs)
  • Right click node_modules folder and go to properties
  • Click the Security tab and advanced
  • At the top you will see "Owner: SYSTEM", click change
  • Enter the user you want permissions for and click Ok
  • Check the box at the bottom of the advanced settings "Replace all child object permission entries with inheritable permission entries from this object" and click ok
  • Do whatever npm install/update you need
查看更多
登录 后发表回答