概要
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
→ インストールの過程でアクセスする一部のディレクトリのアクセスを拒否された模様。
Missing write access to /usr/local/lib/node_modules
原因
特定のディレクトリにパーミッション(権限)がないから。
解決策
解決方法は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」つけるのが抵抗あるなら❷、そんなの気にしない or よくわからないなら今は❶で良いと思われる。
結果
再度インストールを実行してみた。
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
ちょっと警告は出たけど、インストール成功。
補足
「解決策❷」で対応する場合、
最初は純粋に、/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/ ディレクトリ配下の他のディレクトリにも権限与えないとエラーが解消しないと気付き、上記の解決策に至ったとさ。。。
↧