npm throws error without sudo

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

提交回复
热议问题