npm throws error without sudo

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

    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
      

提交回复
热议问题