6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

macOSのnpmでグローバルインストールに失敗する

Posted at

事象

  1. -gオプション付きのnpmコマンドでパッケージをグローバルインストールしようとすると、次のエラーが発生する。

    % npm install -g typescript
    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 ERR!     /Users/foobar/.npm/_logs/2020-04-11T18_25_19_969Z-debug.log
    

原因

  1. NPMの公式サイトでアナウンスされています。
    https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally

  2. macOSのデフォルトでは、次のように設定されています。

    % npm config get prefix
    /usr/local
    

対処方法

  1. 次のコマンドを実行する。

    % mkdir ~/.npm-global
    % npm config set prefix '~/.npm-global'
    
  2. ~/.profileファイルを編集し、パスを通す。

    % vi ~/.profile
    
    export PATH=~/.npm-global/bin:$PATH
    
  3. ~/.profileファイルの内容をシェルに適用する。

    % source ~/.profile
    

動作確認

  1. エラーが発生しなくなることを確認する。

    % npm install -g typescript
    /Users/foobar/.npm-global/bin/tsserver -> /Users/foobar/.npm-global/lib/node_modules/typescript/bin/tsserver
    /Users/foobar/.npm-global/bin/tsc -> /Users/foobar/.npm-global/lib/node_modules/typescript/bin/tsc
    + typescript@3.8.3
    added 1 package from 1 contributor in 0.75s
    
    % tsc --version
    Version 3.8.3
    
6
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?