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:18

In my case,it's because of the permission of ~/tmp.So I do:

sudo chown -R $USER ~/tmp

And it's OK!

查看更多
柔情千种
3楼-- · 2018-12-31 03:19

Also you will need the write permission in node_modules directory:

sudo chown -R $USER /usr/local/lib/node_modules
查看更多
不流泪的眼
4楼-- · 2018-12-31 03:20

This looks like a permissions issue in your home directory. To reclaim ownership of the .npm directory execute:

sudo chown -R $(whoami) ~/.npm
查看更多
春风洒进眼中
5楼-- · 2018-12-31 03:20

Other answers are suggesting to change ownerships or permissions of system directories to a specific user. I highly disadvise from doing so, this can become very awkward and might mess up the entire system!

Here is a more generic and safer approach that supports multi-user as well.

Create a new group for node-users and add the required users to this group. Then set the ownership of node-dependant files/directories to this group.

# Create new group
sudo groupadd nodegrp 

# Add user to group (logname is a variable and gets replaced by the currently logged in user)
sudo usermod -a -G nodegrp `logname`

# Instant access to group without re-login
newgrp nodegrp

# Check group - nodegrp should be listed as well now
groups

# Change group of node_modules, node, npm to new group 
sudo chgrp -R nodegrp /usr/lib/node_modules/
sudo chgrp nodegrp /usr/bin/node
sudo chgrp nodegrp /usr/bin/npm

# (You may want to change a couple of more files (like grunt etc) in your /usr/bin/ directory.)

Now you can easily install your modules as user

npm install -g generator-angular

Some modules (grunt, bower, yo etc.) will still need to be installed as root. This is because they create symlinks in /user/bin/.

Edit

3 years later I'd recommend to use Node Version Manager. It safes you a lot of time and trouble.

查看更多
冷夜・残月
6楼-- · 2018-12-31 03:21

Actually, I was also having the same problem. I was running Ubuntu. Mine problem arises because I'd lost my public key of the Ubuntu. Even updating my system was not happening. It was giving GPG error. In that case, you can regain your key by using this command:

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys <key in GPG error>

After that npm works fine!

查看更多
临风纵饮
7楼-- · 2018-12-31 03:21

If something like below pops up during npm install or Want to install packages globally on a Mac use Sudo

npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall access

查看更多
登录 后发表回答