Resolving EACCES permissions errors when installing packages globally | npm Docs

原文链接: https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally#manually-change-npms-default-directory

This is the best way to avoid permissions issues. To reinstall npm with a node version manager, follow the steps in “Downloading and installing Node.js and npm”. You do not need to remove your current version of npm or Node.js before installing a node version manager.

Manually change npm’s default directory

Note: This section does not apply to Microsoft Windows.

To minimize the chance of permissions errors, you can configure npm to use a different directory. In this example, you will create and use hidden directory in your home directory.

Back up your computer.
  1. mkdir -p ~/.npm-global/lib

    On the command line, in your home directory, create a directory for global installations:

  2. npm config set prefix '~/.npm-global'

    Configure npm to use the new directory path:

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

    In your preferred text editor, open or create a ~/.profile file and add this line:

  4. source ~/.profile

    On the command line, update your system variables:

  5. npm install -g jshint

    To test your new configuration, install a package globally without using sudo:

Instead of steps 3-5, you can use the corresponding ENV variable (e.g. if you don’t want to modify ~/.profile):

NPM_CONFIG_PREFIX=~/.npm-global