npm install -g 実行時に「npm WARN checkPermissions Missing write access to」が発生した場合の対処法

  • 作成日 2020.03.17
  • 更新日 2020.07.22
  • npm
npm install -g 実行時に「npm WARN checkPermissions Missing write access to」が発生した場合の対処法

グローバルにnpmでinstallする際にPermissionsエラーが発生した場合の対処法

環境

  • OS  CentOS 8.0.1905 (Core)
  • node v12.13.1
  • npm 6.13.2

エラーメッセージ

npm i -g [パッケージ名]

実行時に発生

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!   stack: "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 config get prefix

下記のコマンドで権限の付与が可能です。

sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}