npm throws error without sudo

前端 未结 30 1737
清酒与你
清酒与你 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:09

    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.

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

    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/

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

    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

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

    @Yves M.'s answer was very similar to my solution. Here are the commands I used, which were slightly different from his.

    curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.0/install.sh | bash
    

    Then query for the latest version:

    nvm ls-remote
    

    Then install the newest version:

    nvm install YOUR_VERSION_HERE
    

    example

    nvm install v5.8.0
    
    0 讨论(0)
  • 2020-11-21 08:10

    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 messed up npm.

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

    Best solution would be this which is provided by npm documentation.


    For Ubuntu suggested solution is Option#2

    Brief steps:
    Make a directory for global installations:
    mkdir ~/.npm-global

    Configure npm to use the new directory path:
    npm config set prefix '~/.npm-global'
    npm config get prefix can help you to verify if prefix was updated or not. The result would be <Your Home Directory>/.npm-global

    Open or create a ~/.profile file and add this line:
    export PATH=~/.npm-global/bin:$PATH

    Back on the command line, update your system variables:
    source ~/.profile

    Instead of steps 2-4 you can also use the corresponding ENV variable (e.g. if you don't want to modify ~/.profile):

    NPM_CONFIG_PREFIX=~/.npm-global


    For Mac suggested solution is Option#3

    On Mac OS you can avoid this problem altogether by using the Homebrew package manager

    brew install node

    0 讨论(0)
提交回复
热议问题