无法在 macOS Sierra 上安装 ng CLI

Not able to install ng CLI on macOS Sierra

昨天我全新安装了 macOS Sierra,当我尝试安装 ng CLI 时出现此错误。

Error: EACCES: permission denied, rmdir '/usr/local/lib/node_modules/.staging'

我尝试过 sudo uninstallnpm clean cache 并重新安装,但没有成功。我该如何解决这个问题?

我能够使用 option 2 in the NPM documentation:

修复错误

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` 
    

Test: Download a package globally without using sudo.

    npm install -g jshint

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