permission denied エラーの解決法
Error: EACCES: permission denied, open '/path/to/file'bash: /usr/local/bin/npm: Permission deniedError: ENOENT: no such file or directory(実はアクセス権の問題)- Claude Codeが特定のコマンドを実行しようとして拒否される
※npm: Node.jsのパッケージ管理ツール。`npm install` でライブラリをインストールする。
原因1: ファイル・ディレクトリのOS権限
Section titled “原因1: ファイル・ディレクトリのOS権限”最もよくある原因です。ファイルが root 所有になっていたり、実行権限がなかったりします。
# 権限確認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 所有の場合、一般ユーザーではインストールできません。
原因4: Windowsのファイルロック
Section titled “原因4: Windowsのファイルロック”Windows環境では、別のプロセスがファイルを開いている場合、書き込みができません。
ファイル権限の修正(Linux/macOS)
Section titled “ファイル権限の修正(Linux/macOS)”# 自分のファイルに権限を付与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のグローバルインストール問題の解決”# 方法1: npmのグローバルディレクトリを変更(推奨)mkdir ~/.npm-globalnpm 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の権限問題”# 権限を修正sudo chown -R $(whoami) $(brew --prefix)/*
# または特定のディレクトリのみsudo chown -R $(whoami) /usr/local/lib/node_modulesWindowsのファイルロック問題
Section titled “Windowsのファイルロック問題”# ファイルを使用しているプロセスを確認# (管理者権限のPowerShell)Get-Process | Where-Object {$_.MainWindowTitle -like "*filename*"}
# または: Sysinternalsのプロセスエクスプローラーを使うよくある原因:
- エディタが対象ファイルを開いたまま
node_modulesを別のターミナルが使っている- ウイルス対策ソフトがファイルをスキャン中
解決策: 該当のプロセスを終了してから再試行します。
Claude Codeの権限エラーの詳細確認
Section titled “Claude Codeの権限エラーの詳細確認”Claude Codeが何を拒否しているかを確認するには:
# ログを確認cat ~/.claude/logs/claude-code.log | grep "permission"
# または詳細モードで実行claude --verboseClaude Codeが実行しようとしたコマンドと、拒否された理由が表示されます。
プロジェクト専用の settings.json を作成する
Section titled “プロジェクト専用の 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 に関するトラブルを避ける”# .gitignore に追加(当然だが)echo "node_modules/" >> .gitignore
# node_modules の権限をリセットrm -rf node_modules/npm install