当使用 npm install -g xxx(全局安装)出现错误时的处理方法

简要概述

在进行全局安装npm时,如果遇到安装失败的错误,以下是解决方法的备忘录。

当在运行”npm install -g @angular/cli”命令时遇到错误并且无法正常完成时,我们解决了权限问题,成功地安装了该软件。

前提 – 这是在讨论之前必须先确定的条件。

    • Node.js をインストール済み

 

    • 筆者の環境

macOS 10.15.7 Catalina
Node.js v14.16.1
npm v6.14.12

事件

在执行以下命令时,尝试使用npm全局安装”Angular CLI”时的情况。

# -g の後ろは何でも良いが、今回は実例としてこのパッケージをインストール
npm install -g @angular/cli

然后,发生了以下错误。

npm WARN deprecated request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142
npm WARN deprecated har-validator@5.1.5: this library is no longer supported
npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules
npm ERR! code EACCES
npm ERR! syscall access
npm ERR! path /usr/local/lib/node_modules
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, access '/usr/local/lib/node_modules'
npm ERR!  [Error: EACCES: permission denied, access '/usr/local/lib/node_modules'] {
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'access',
npm ERR!   path: '/usr/local/lib/node_modules'
npm ERR! }
npm ERR! 
npm ERR! The operation was rejected by your operating system.
npm ERR! It is likely you do not have the permissions to access this file as the current user
npm ERR! 
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/{ユーザ名}/.npm/_logs/2021-04-08T17_35_24_683Z-debug.log

→ 在安装过程中,似乎被拒绝访问某些目录的访问权限。
无法写入 /usr/local/lib/node_modules。

Only need one option.

由于特定目录缺乏权限的原因。

解决方案

有两个解决方法:
1. 在安装命令前面加上“sudo”进行安装。
2. 在安装过程中使用sudo。

sudo npm install -g @angular/cli

将指定目录的所有者更改为当前用户。

# /usr/local/ 以下の lib/node_modules, bin, share のオーナーを現在のユーザに変更
sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}

无论选哪个都可以,但如果每次接触全球包都要加上“sudo”感到抵触的话❷,就不必在意这个了,或者如果不太清楚的话,现在应该可以选择❶。

结果 = jié guǒ

我尝试重新安装了一次。

npm WARN deprecated request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142
npm WARN deprecated har-validator@5.1.5: this library is no longer supported
/usr/local/bin/ng -> /usr/local/lib/node_modules/@angular/cli/bin/ng

> @angular/cli@11.2.8 postinstall /usr/local/lib/node_modules/@angular/cli
> node ./bin/postinstall/script.js

+ @angular/cli@11.2.8
updated 1 package in 4.667s

虽然有些警告,但是安装成功。

添加或填補所缺乏的部分。

如果选择使用「解决策2」进行处理,
一开始我尝试了纯粹地给予 /usr/local/lib/node_modules 目录下的以下命令权限,但是、、、

sudo chown -R $USER /usr/local/lib/node_modules

然而,当我重新运行安装程序后,发生了以下情况,这一次甚至在其他目录也无法访问。

npm ERR! code EACCES
npm ERR! syscall symlink
npm ERR! path ../lib/node_modules/@angular/cli/bin/ng
npm ERR! dest /usr/local/bin/ng
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, symlink '../lib/node_modules/@angular/cli/bin/ng' -> '/usr/local/bin/ng'
npm ERR!  [OperationalError: EACCES: permission denied, symlink '../lib/node_modules/@angular/cli/bin/ng' -> '/usr/local/bin/ng'] {
npm ERR!   cause: [Error: EACCES: permission denied, symlink '../lib/node_modules/@angular/cli/bin/ng' -> '/usr/local/bin/ng'] {
npm ERR!     errno: -13,
npm ERR!     code: 'EACCES',
npm ERR!     syscall: 'symlink',
npm ERR!     path: '../lib/node_modules/@angular/cli/bin/ng',
npm ERR!     dest: '/usr/local/bin/ng'
npm ERR!   },
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'symlink',
npm ERR!   path: '../lib/node_modules/@angular/cli/bin/ng',
npm ERR!   dest: '/usr/local/bin/ng'
npm ERR! }

所以,我注意到如果不给予/usr/local/目录下的其他目录权限,错误就无法解决,然后得到了上述解决方案。

bannerAds