9
0

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 1 year has passed since last update.

EACCES: permission denied が出るので、nvmを使うようにした

Posted at

はじめに

npmを使ってグローバルにインストールしようとした時、以下のようなエラーが出力されました。
初めて出会いましたが、もう2度と会うことはなさそうなので記事に残します。

  % npm install -g typescript
  npm ERR! code EACCES
  npm ERR! syscall rename
  npm ERR! path /usr/local/lib/node_modules/npm
  npm ERR! dest /usr/local/lib/node_modules/.npm-i9nnxROI
  npm ERR! errno -13
  npm ERR! Error: EACCES: permission denied, rename '/usr/local/lib/node_modules/npm' -> 
  '/usr/local/lib/node_modules/.npm-i9nnxROI'
  npm ERR!  [Error: EACCES: permission denied, rename '/usr/local/lib/node_modules/npm' -> 
  '/usr/local/lib/node_modules/.npm-i9nnxROI'] {
  npm ERR!   errno: -13,
  npm ERR!   code: 'EACCES',
  npm ERR!   syscall: 'rename',
  npm ERR!   path: '/usr/local/lib/node_modules/npm',
  npm ERR!   dest: '/usr/local/lib/node_modules/.npm-i9nnxROI'
  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

EACCES

原因

To publish and install packages to and from the public npm registry or a private npm registry, you must install Node.js and the npm command line interface using either a Node version manager or a Node installer. We strongly recommend using a Node version manager like nvm to install Node.js and npm. We do not recommend using a Node installer, since the Node installation process installs npm in a directory with local permissions and can cause permissions errors when you run npm packages globally.

nodeのinstallerを使ってnodeをインストールする場合、local permissionのディレクトリにインストールしてしまうため、npmのパッケージをグローバルにインストールしようとするとこのエラーが発生してしまうようです。

解決方法

これの解決方法はnpm公式にがっつり書かれていて、以下の2択です。

  • nodeのバージョンマネージャーを使ってnpmを再インストールする
    • (原文) Reinstall npm with a node version manager (recommended)
  • npmのデフォルトのディレクトリを変更する
    • (原文) Manually change npm's default directory

今回は前者の「nodeのバージョンマネージャーを使ってnpmを再インストールする」にします。

nvmをいれる

Nodeのバージョンマネージャーは、nvm, n, nodenv などいくつかありますが、僕は長いものに巻かれるタイプなのでnvmを使うことにします。
使い方に関しては、https://github.com/nvm-sh/nvm#installing-and-updating に書かれているままです。

  % curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash

export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm

上記を~/.zshrcに追加(※使っているシェルによりけり)

  % source ~/.zshrc

  • 最新バージョンのnodeをいれる場合
  % nvm install node
  • バージョン指定していれる場合
  % nvm install 14.7.0 # or 16.3.0, 12.22.1, etc
9
0
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
9
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?