【Bug已解决】Claude: command not found / Claude CLI not installed properly — Claude CLI 安装失败解决方案1. 问题描述安装 Claude Code 后无法找到命令或运行失败# 命令未找到 $ claude zsh: command not found: claude # 或 npm 安装失败 $ npm install -g anthropic-ai/claude-code Error: EACCES: permission denied # 或安装后无法运行 $ claude --version Error: Cannot find module anthropic-ai/claude-code # 或路径问题 $ which claude # 无输出这个问题在以下场景中特别常见npm 全局安装路径不在 PATH权限不足EACCESNode.js 版本过低npm 缓存损坏网络问题导致安装失败系统兼容性问题2. 原因分析原因分类表原因分类具体表现占比PATH 问题找不到命令约 35%权限不足EACCES约 25%Node 版本低 18约 15%npm 缓存损坏安装异常约 10%网络问题下载失败约 10%系统兼容不支持约 5%3. 解决方案方案一修复 PATH最推荐# 步骤 1找到 npm 全局路径 npm config get prefix # 通常是 /usr/local 或 ~/.npm-global # 步骤 2添加到 PATH export PATH$(npm config get prefix)/bin:$PATH # 步骤 3永久设置 echo export PATH$(npm config get prefix)/bin:$PATH ~/.zshrc source ~/.zshrc # 步骤 4验证 which claude claude --version方案二修复权限# 步骤 1使用 sudo 安装 sudo npm install -g anthropic-ai/claude-code # 步骤 2或使用 nvm推荐 curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash nvm install 20 nvm use 20 npm install -g anthropic-ai/claude-code # 步骤 3或配置 npm 全局路径 mkdir -p ~/.npm-global npm config set prefix ~/.npm-global npm install -g anthropic-ai/claude-code # 步骤 4验证 which claude claude --version方案三检查 Node.js 版本# 步骤 1检查版本 node --version # 需要 18 # 步骤 2如果版本低更新 nvm install 20 nvm use 20 # 步骤 3或使用 HomebrewmacOS brew install node # 步骤 4重新安装 Claude Code npm install -g anthropic-ai/claude-code claude --version方案四清除 npm 缓存# 步骤 1清除缓存 npm cache clean --force # 步骤 2卸载旧版本 npm uninstall -g anthropic-ai/claude-code # 步骤 3重新安装 npm install -g anthropic-ai/claude-code # 步骤 4验证 which claude claude --version方案五使用 npx 运行# 步骤 1不安装直接用 npx npx anthropic-ai/claude-code task # 步骤 2或指定版本 npx anthropic-ai/claude-codelatest task # 步骤 3验证 npx anthropic-ai/claude-code --version # 步骤 4如果可用再全局安装 npm install -g anthropic-ai/claude-code方案六手动安装# 步骤 1下载 npm pack anthropic-ai/claude-code # 步骤 2解压 tar -xzf anthropic-ai-claude-code-*.tgz cd package # 步骤 3安装依赖 npm install # 步骤 4链接 npm link claude --version4. 各方案对比总结方案适用场景推荐指数难度方案一PATH找不到⭐⭐⭐⭐⭐低方案二权限EACCES⭐⭐⭐⭐⭐低方案三Node 版本版本低⭐⭐⭐⭐⭐低方案四清缓存安装异常⭐⭐⭐⭐⭐低方案五npx临时⭐⭐⭐⭐低方案六手动严重⭐⭐⭐中5. 常见问题 FAQ5.1 如何检查 Claude Code 是否安装which claude claude --version5.2 npm 全局路径在哪npm config get prefix # 通常 /usr/local 或 ~/.npm-global5.3 如何修复 PATHexport PATH$(npm config get prefix)/bin:$PATH # 添加到 ~/.zshrc5.4 EACCES 权限错误使用sudo或配置 npm 全局路径到用户目录或使用 nvm。5.5 Node.js 需要什么版本Node.js 18 或更高。5.6 如何使用 nvmnvm install 20 nvm use 20 npm install -g anthropic-ai/claude-code5.7 npm 缓存损坏npm cache clean --force npm install -g anthropic-ai/claude-code5.8 可以用 npx 吗npx anthropic-ai/claude-code task5.9 macOS 如何安装 Node.jsbrew install node # 或使用 nvm5.10 排查清单速查表□ 1. which claude 检查 □ 2. npm config get prefix 查路径 □ 3. export PATH 添加到 ~/.zshrc □ 4. node --version 检查 18 □ 5. nvm install 20 更新 Node □ 6. sudo npm install 修复权限 □ 7. npm cache clean --force 清缓存 □ 8. npx anthropic-ai/claude-code 临时 □ 9. brew install node (macOS) □ 10. npm uninstall install 重装6. 总结根本原因安装失败最常见原因是 PATH 问题35%和权限不足25%EACCES最佳实践使用 nvm 管理 Node.jsnpm install -g anthropic-ai/claude-codePATH 修复export PATH$(npm config get prefix)/bin:$PATH添加到~/.zshrc权限修复使用 nvm 或配置 npm 全局路径到用户目录避免 sudo最佳实践建议使用 nvm 安装 Node.js 20npm cache clean --force后重新安装故障排查流程图flowchart TD A[command not found] -- B[which claude 检查] B -- C{找到?} C --|否| D[npm config get prefix] D -- E[export PATH 添加] E -- F[claude --version 验证] C --|是| F F -- G{成功?} G --|是| H[✅ 问题解决] G --|否| I[node --version] I -- j{Node 18?} j --|否| K[nvm install 20] j --|是| L[npm cache clean] K -- M[npm install -g claude-code] L -- M M -- F F -- N{成功?} N --|是| H N --|否| O[检查权限] O -- P{EACCES?} P --|是| Q[sudo 或 nvm] P --|否| R[npx 临时运行] Q -- S[npm config set prefix ~/.npm-global] S -- M R -- T[npx anthropic-ai/claude-code] T -- H M -- U{成功?} U --|是| H U --|否| V[手动安装] V -- W[npm pack tar npm link] W -- H H -- X[长期: nvm PATH npm install] X -- Y[✅ 长期方案]