npm throws error without sudo

前端 未结 30 1923
清酒与你
清酒与你 2020-11-21 07:43

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 c

相关标签:
30条回答
  • 2020-11-21 08:11

    This is the solution I utilized and worked. I tried utilizing whoami never worked.

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

    then

    sudo chown -R $USER /usr/local/bin/npm

    then

    sudo chown -R $USER /usr/local/bin/node

    0 讨论(0)
  • 2020-11-21 08:12

    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
    0 讨论(0)
  • 2020-11-21 08:13

    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.

    0 讨论(0)
  • 2020-11-21 08:13

    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
    
    0 讨论(0)
  • 2020-11-21 08:15

    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!

    0 讨论(0)
  • 2020-11-21 08:18

    Also you will need the write permission in node_modules directory:

    sudo chown -R $USER /usr/local/lib/node_modules
    
    0 讨论(0)
提交回复
热议问题