Skip to content

permission denied エラーの解決法

  • Error: EACCES: permission denied, open '/path/to/file'
  • bash: /usr/local/bin/npm: Permission denied
  • Error: ENOENT: no such file or directory(実はアクセス権の問題)
  • Claude Codeが特定のコマンドを実行しようとして拒否される
npm: Node.jsのパッケージ管理ツール。`npm install` でライブラリをインストールする。

原因1: ファイル・ディレクトリのOS権限

Section titled “原因1: ファイル・ディレクトリのOS権限”

最もよくある原因です。ファイルが root 所有になっていたり、実行権限がなかったりします。

Terminal window
# 権限確認
ls -la /path/to/file
# 例:
# -rwxr-xr-x root root 12345 ... ← rootのみ書き込み可
# ---------- user user 5678 ... ← 全員が読み取り不可

原因2: Claude Codeの設定で許可されていないコマンド

Section titled “原因2: Claude Codeの設定で許可されていないコマンド”

settings.json でコマンドが許可されていない場合、Claude Codeが自動的にブロックします。これはセキュリティのための意図的な動作です。

原因3: npmやbunのグローバルインストール権限

Section titled “原因3: npmやbunのグローバルインストール権限”

グローバルパッケージのディレクトリが root 所有の場合、一般ユーザーではインストールできません。

Windows環境では、別のプロセスがファイルを開いている場合、書き込みができません。

ファイル権限の修正(Linux/macOS)

Section titled “ファイル権限の修正(Linux/macOS)”
Terminal window
# 自分のファイルに権限を付与
chmod 644 filename # 読み書き(所有者)、読み取り(他)
chmod 755 directory/ # 実行可能なディレクトリ
chmod +x script.sh # 実行権限を付与
# 再帰的に適用
chmod -R 755 ./src/
# 所有者の変更(rootが必要)
sudo chown -R $USER:$USER ./node_modules/

Claude Codeの設定でコマンドを許可する

Section titled “Claude Codeの設定でコマンドを許可する”

~/.claude/settings.json(グローバル)または .claude/settings.json(プロジェクト)に許可を追加します。

{
"permissions": {
"allow": [
"Bash(npm install)",
"Bash(npm run *)",
"Bash(npx *)",
"Bash(git *)"
]
}
}

npmのグローバルインストール問題の解決

Section titled “npmのグローバルインストール問題の解決”
Terminal window
# 方法1: npmのグローバルディレクトリを変更(推奨)
mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
# .bashrc または .zshrc に追加
export PATH=~/.npm-global/bin:$PATH
# 方法2: nvm を使う(最も安全)
# nvmを使うとNode.jsのパス全体がユーザーディレクトリになる
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
# 方法3: sudo なしのローカルインストール(プロジェクト専用)
npm install --save-dev [package] # グローバルでなくローカルに
npx [command] # インストールせずに実行

macOSの場合: Homebrewで入れたNode.jsの権限問題

Section titled “macOSの場合: Homebrewで入れたNode.jsの権限問題”
Terminal window
# 権限を修正
sudo chown -R $(whoami) $(brew --prefix)/*
# または特定のディレクトリのみ
sudo chown -R $(whoami) /usr/local/lib/node_modules
Terminal window
# ファイルを使用しているプロセスを確認
# (管理者権限のPowerShell)
Get-Process | Where-Object {$_.MainWindowTitle -like "*filename*"}
# または: Sysinternalsのプロセスエクスプローラーを使う

よくある原因:

  • エディタが対象ファイルを開いたまま
  • node_modules を別のターミナルが使っている
  • ウイルス対策ソフトがファイルをスキャン中

解決策: 該当のプロセスを終了してから再試行します。

Claude Codeの権限エラーの詳細確認

Section titled “Claude Codeの権限エラーの詳細確認”

Claude Codeが何を拒否しているかを確認するには:

Terminal window
# ログを確認
cat ~/.claude/logs/claude-code.log | grep "permission"
# または詳細モードで実行
claude --verbose

Claude Codeが実行しようとしたコマンドと、拒否された理由が表示されます。

プロジェクト専用の settings.json を作成する

Section titled “プロジェクト専用の settings.json を作成する”
.claude/settings.json
{
"permissions": {
"allow": [
"Bash(npm *)",
"Bash(npx *)",
"Bash(git add *)",
"Bash(git commit *)",
"Bash(git checkout *)"
],
"deny": [
"Bash(rm -rf *)",
"Bash(sudo *)"
]
}
}

node_modules に関するトラブルを避ける

Section titled “node_modules に関するトラブルを避ける”
Terminal window
# .gitignore に追加(当然だが)
echo "node_modules/" >> .gitignore
# node_modules の権限をリセット
rm -rf node_modules/
npm install