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

Changing the owner on "system-global" folders is a hack. On a fresh install, I would configure NPM to use an already writable location for "user-global" programs:

npm config set prefix ~/npm

Then make sure you add that folder to your path:

export PATH="$PATH:$HOME/npm/bin"

See @ErikAndreas' answer to NPM modules won't install globally without sudo and longer step-by-step guide by @sindresorhus with also sets $MANPATH.

查看更多
骚的不知所云
3楼-- · 2018-12-31 03:04

ISSUE: You (the user) don't have the right set of permissions for the directory.

The instant way out is to run the npm install using sudo, but this may give you the same error, or improper installation.

AND changing directory ownership is not a good option, a temporary patch.


Solution/Suggestion: Change npm's Default Directory (from official docs)

Back-up your computer before moving forward.

(optional) In case you have a erroneous installation, first uninstall it:

npm uninstall <package-name>  # use sudo if you used it while installation
npm cache verify  # or, npm cache clean for npm version below 5.x.x 
  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 or ~/.bash_profile file and add this line:

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

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

    source ~/.profile

  5. (optional) Test: Download a package globally without using sudo.

    npm install -g jshint

查看更多
看风景的人
4楼-- · 2018-12-31 03:05

As if we need more answers here, but anyway..

Sindre Sorus has a guide Install npm packages globally without sudo on OS X and Linux outlining how to cleanly install without messing with permissions:

Here is a way to install packages globally for a given user.

  1. Create a directory for your global packages

    mkdir "${HOME}/.npm-packages"
    
  2. Reference this directory for future usage in your .bashrc/.zshrc:

    NPM_PACKAGES="${HOME}/.npm-packages"
    
  3. Indicate to npm where to store your globally installed package. In your $HOME/.npmrc file add:

    prefix=${HOME}/.npm-packages
    
  4. Ensure node will find them. Add the following to your .bashrc/.zshrc:

    NODE_PATH="$NPM_PACKAGES/lib/node_modules:$NODE_PATH"
    
  5. Ensure you'll find installed binaries and man pages. Add the following to your .bashrc/.zshrc:

    PATH="$NPM_PACKAGES/bin:$PATH"
    # Unset manpath so we can inherit from /etc/manpath via the `manpath`
    # command
    unset MANPATH # delete if you already modified MANPATH elsewhere in your config
    MANPATH="$NPM_PACKAGES/share/man:$(manpath)"
    

Check out npm-g_nosudo for doing the above steps automagically

Checkout the source of this guide for the latest updates.

查看更多
与君花间醉酒
5楼-- · 2018-12-31 03:05

Another great fix here to configure NPM properly, run the following commands :

npm config set prefix '~/.npm_packages'
PATH=$PATH:$HOME/.npm_packages/bin; export PATH
查看更多
唯独是你
6楼-- · 2018-12-31 03:06

Permissions you used when installing Node will be required when doing things like writing in your npm directory (npm link, npm install -g, etc.).

You probably ran node installation with root permissions, that's why the global package installation is asking you to be root.


Solution 1: NVM

Don't hack with permissions, install node the right way.

On a development machine, you should not install and run node with root permissions, otherwise things like npm link, npm install -g will need the same permissions.

NVM (Node Version Manager) allows you to install Node without root permissions and also allows you to install many versions of Node to play easily with them.. Perfect for development.

  1. Uninstall Node (root permission will probably be required). This might help you.
  2. Then install NVM following instructions on this page.
  3. Install Node via NVM: nvm install node

Now npm link, npm install -g will no longer require you to be root.

Edit: See also https://docs.npmjs.com/getting-started/fixing-npm-permissions


Solution 2: Install packages globally for a given user

Don't hack with permissions, install npm packages globally the right way.

If you are on OSX or Linux, you can create a user dedicated directory for your global package and setup npm and node to know how to find globally installed packages.

Check out this great article for step by step instructions on installing npm modules globally without sudo.

See also: npm's documentation on Fixing npm permissions.

查看更多
闭嘴吧你
7楼-- · 2018-12-31 03:07

In case sudo chown -R $(whoami) ~/.npm didn't work for you, or you need a non terminal command solution.

The issue is that your user account does not have write permission to node_modules folder, so you can do the following

  1. Open finder and press cmd + shift + g this will open go to folder with url

  2. Write the following path /usr/local/lib/node_modules and press go

  3. Right click on node_modules folder and choose Get Info

  4. Scroll down to sharing & permissions section

  5. Unlock to be able to make changes.

  6. Press + and add your user account

  7. Make sure that you choose Read & Write in privilege drop down

Now you should be able to install packages without sudo and permission issues should be solved

查看更多
登录 后发表回答