Claude Code Update npm:npmを使った更新手順と注意点

Claude Codeをnpmで管理しているけれど、更新がうまくいかないと悩んでいませんか?本記事では、npmを使ったClaude Codeの更新方法と、発生しやすい問題の解決策を詳しく解説します。

34 min read

npmでClaude Codeを更新する基本

標準的な更新コマンド

# グローバルパッケージとして更新
npm update -g @anthropic-ai/claude-code

バージョンを指定して更新

# 最新の安定版
npm install -g @anthropic-ai/claude-code@latest

# 特定のバージョン
npm install -g @anthropic-ai/claude-code@1.0.50

# ベータ版(利用可能な場合)
npm install -g @anthropic-ai/claude-code@beta

npmコマンドの詳細

現在の状態を確認

# インストール済みのグローバルパッケージを確認
npm list -g --depth=0

# Claude Codeのバージョン確認
npm list -g @anthropic-ai/claude-code

# 利用可能な更新を確認
npm outdated -g @anthropic-ai/claude-code

出力例:

Package                    Current  Wanted  Latest  Location
@anthropic-ai/claude-code  1.0.45   1.0.50  1.0.50  global

利用可能なバージョン一覧

# すべてのバージョン
npm view @anthropic-ai/claude-code versions

# 最新の10バージョン
npm view @anthropic-ai/claude-code versions --json | jq '.[-10:]'

# 最新バージョンのみ
npm view @anthropic-ai/claude-code version

環境別の更新手順

macOS

# 標準的な更新
npm update -g @anthropic-ai/claude-code

# 権限エラーの場合
sudo npm update -g @anthropic-ai/claude-code

Linux(Ubuntu/Debian)

# 通常の更新
npm update -g @anthropic-ai/claude-code

# sudoが必要な場合
sudo npm update -g @anthropic-ai/claude-code

# nvm使用時はsudo不要
npm update -g @anthropic-ai/claude-code

Windows

PowerShell(管理者として実行):

# 更新コマンド
npm update -g @anthropic-ai/claude-code

管理者権限なしでエラーになる場合:

  1. 「スタート」を右クリック
  2. 「Windows PowerShell(管理者)」を選択
  3. 更新コマンドを実行

よくある問題と解決策

問題1:EACCES権限エラー

エラーメッセージ:

npm ERR! Error: EACCES: permission denied, access '/usr/local/lib/node_modules'

解決策1:sudoを使用

sudo npm update -g @anthropic-ai/claude-code

解決策2:npmのディレクトリ権限を変更

# ディレクトリの所有者を変更
sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}

解決策3:ユーザーディレクトリにインストール

# 設定
mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc
source ~/.bashrc

# 再インストール
npm install -g @anthropic-ai/claude-code

問題2:npm ERR! code ENETUNREACH

エラーメッセージ:

npm ERR! code ENETUNREACH
npm ERR! network request to https://registry.npmjs.org failed

解決策:

# ネットワーク接続を確認
ping registry.npmjs.org

# プロキシ設定(企業ネットワークの場合)
npm config set proxy http://proxy.example.com:8080
npm config set https-proxy http://proxy.example.com:8080

# SSL問題の場合(非推奨、テスト用)
npm config set strict-ssl false

問題3:npm ERR! code ENOENT

エラーメッセージ:

npm ERR! code ENOENT
npm ERR! syscall spawn

解決策:

# npmキャッシュをクリア
npm cache clean --force

# 再インストール
npm install -g @anthropic-ai/claude-code@latest

問題4:バージョンが更新されない

症状:

$ npm update -g @anthropic-ai/claude-code
$ claude --version
# 古いバージョンが表示される

解決策:

# 完全に削除して再インストール
npm uninstall -g @anthropic-ai/claude-code
npm cache clean --force
npm install -g @anthropic-ai/claude-code@latest

問題5:Node.jsバージョンの互換性

エラーメッセージ:

npm WARN engine @anthropic-ai/claude-code: wanted: {"node":">=18.0.0"}

解決策:

# Node.jsのバージョン確認
node --version

# nvmでバージョン切り替え
nvm install 20
nvm use 20

# 再度更新
npm update -g @anthropic-ai/claude-code

npm設定の最適化

推奨設定

# 進捗表示を有効化
npm config set progress true

# ログレベルの設定
npm config set loglevel info

# 監査を有効化
npm config set audit true

# 設定確認
npm config list

プロキシ環境での設定

# プロキシ設定
npm config set proxy http://proxy.example.com:8080
npm config set https-proxy http://proxy.example.com:8080

# 認証付きプロキシ
npm config set proxy http://user:pass@proxy.example.com:8080

# プロキシを解除
npm config delete proxy
npm config delete https-proxy

更新の自動化

スクリプトによる更新

update-claude.sh:

#!/bin/bash

echo "Claude Code Update Script"
echo "========================="

# 現在のバージョン
CURRENT=$(npm list -g @anthropic-ai/claude-code --json 2>/dev/null | grep -o '"version": "[^"]*"' | head -1 | cut -d'"' -f4)
LATEST=$(npm view @anthropic-ai/claude-code version 2>/dev/null)

echo "Current version: $CURRENT"
echo "Latest version: $LATEST"

if [ "$CURRENT" = "$LATEST" ]; then
    echo "Already up to date!"
    exit 0
fi

echo "Updating..."
npm update -g @anthropic-ai/claude-code

if [ $? -eq 0 ]; then
    echo "Update successful!"
    claude --version
else
    echo "Update failed!"
    exit 1
fi

package.jsonでの管理

プロジェクトローカルで管理する場合:

{
  "name": "my-project",
  "devDependencies": {
    "@anthropic-ai/claude-code": "^1.0.50"
  },
  "scripts": {
    "update:claude": "npm update @anthropic-ai/claude-code"
  }
}

npmの代替ツール

yarnを使用

# yarnでグローバルインストール
yarn global add @anthropic-ai/claude-code

# 更新
yarn global upgrade @anthropic-ai/claude-code

pnpmを使用

# pnpmでグローバルインストール
pnpm add -g @anthropic-ai/claude-code

# 更新
pnpm update -g @anthropic-ai/claude-code

ベストプラクティス

1. 定期的な更新チェック

# 週に1回程度実行
npm outdated -g

2. 更新前のバージョン記録

# 更新前のバージョンを記録
npm list -g @anthropic-ai/claude-code > ~/claude-version-before.txt

# 更新実行
npm update -g @anthropic-ai/claude-code

# 更新後のバージョンを記録
npm list -g @anthropic-ai/claude-code > ~/claude-version-after.txt

3. ロールバックの準備

# 以前のバージョンを確認
npm view @anthropic-ai/claude-code versions

# 問題があれば戻す
npm install -g @anthropic-ai/claude-code@1.0.45

4. クリーンな環境での更新

# キャッシュクリア
npm cache clean --force

# 古いバージョンを削除
npm uninstall -g @anthropic-ai/claude-code

# 最新版をインストール
npm install -g @anthropic-ai/claude-code@latest

まとめ

npmでClaude Codeを更新するポイント:

  • 基本コマンドnpm update -g @anthropic-ai/claude-code
  • 権限エラーはsudoまたは設定変更で解決
  • ネットワークエラーはプロキシ設定を確認
  • キャッシュクリアで多くの問題が解決
  • 定期的な更新でセキュリティと機能を維持

適切なnpm更新手順を理解することで、Claude Codeを常に最新の状態で使用できます。

Related Articles