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

John Papa points to the history and reasoning behind this issue and gives a solid fix:

John Papa's steps are to:

  1. Use brew to install node without npm
  2. Update your .bash_profile/.bashrc to let npm and node know where to install and find packages
  3. Use brew to update node and npm to update itself

Hope this helps the curious!

查看更多
梦醉为红颜
3楼-- · 2018-12-31 03:13

Watch OUT!!! Watch OUT!!! Watch OUT!!!

chown or chmod is NOT the solution, because of security-risk.

Instead do this, do:

First check, where npm point to, if you call:

npm config get prefix

If /usr is returned, you can do the following:

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

This create a npm-Direktory in your Home-Directory and point npm to it.

To got this changes permanent, you have to add the export-command to your .bashrc:

echo -e "export NPM_CONFIG_PREFIX=~/.npm-global\nexport PATH=$PATH:~/.npm-global/bin" >> ~/.bashrc
查看更多
柔情千种
4楼-- · 2018-12-31 03:13

The official documentation on how to fix npm install permissions with an EACCES error is located at https://docs.npmjs.com/getting-started/fixing-npm-permissions.

I encountered this problem after a fresh install of node using the .pkg installer on OSX. There are some great answers here, but I didn't see a link to npmjs.com yet.

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.

  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
    
查看更多
孤独寂梦人
5楼-- · 2018-12-31 03:13

Here are the steps, in detail ...

  • Install Node.js from nodejs.org/en/download

  • Update to the latest version of npm: $ npm install npm -g

  • Make a new folder for the npm global packages $ mkdir ~/.npm-packages

  • Tell npm where to find/store them $ npm config set prefix ~/.npm-packages

Verify the install

# this should show the versions
node -v  
npm -v  
# this should show npm and ng with no errors
npm list -g --depth=0

Hope it works for you, it worked for me!

查看更多
梦醉为红颜
6楼-- · 2018-12-31 03:14

When you run npm install -g somepackage, you may get an EACCES error asking you to run the command again as root/Administrator. It's a permissions issue.

It's easy to fix, open your terminal (Applications > Utilities > Terminal)

sudo chown -R $USER /usr/local/lib/node_modules

** I strongly recommend you to not use the package management with sudo (sudo npm -g install something), because you can get some issues later **

Reference: http://foohack.com/2010/08/intro-to-npm/

查看更多
明月照影归
7楼-- · 2018-12-31 03:14

If changing permissions for directory /usr/local/lib/npm_modules not helps, u should add your user to group (in most cases this is staff group), that has rwx rights for /usr/bin directory. Because npm tries to make symlink with /usr/bin/__package__.

P.S. Don't forget to relogin after changing user group

查看更多
登录 后发表回答