Skip to content

Instantly share code, notes, and snippets.

@engalar
Created February 4, 2026 01:47
Show Gist options
  • Select an option

  • Save engalar/4eea8be34bc81130c3c6a5fc119a3b1f to your computer and use it in GitHub Desktop.

Select an option

Save engalar/4eea8be34bc81130c3c6a5fc119a3b1f to your computer and use it in GitHub Desktop.
Claude Code PATH 诊断工具 - 定位 Windows 上 claude.cmd 找不到的问题
# Claude Code PATH 诊断脚本
# 用于定位 Windows 上 claude.cmd 找不到的问题
$ErrorActionPreference = "Continue"
Write-Host "`n========================================" -ForegroundColor Cyan
Write-Host " Claude Code PATH 诊断工具" -ForegroundColor Cyan
Write-Host "========================================`n" -ForegroundColor Cyan
# 1. 检查 claude 命令是否可用
Write-Host "[1] 检查 claude 命令状态..." -ForegroundColor Yellow
$claudeCmd = Get-Command claude -ErrorAction SilentlyContinue
if ($claudeCmd) {
Write-Host " [OK] claude 命令已找到: $($claudeCmd.Source)" -ForegroundColor Green
} else {
Write-Host " [FAIL] claude 命令未找到" -ForegroundColor Red
}
# 2. 搜索 claude.cmd 可能的位置
Write-Host "`n[2] 搜索 claude.cmd 可能的安装位置..." -ForegroundColor Yellow
$searchPaths = @(
# npm 全局路径
"$env:APPDATA\npm",
"$env:LOCALAPPDATA\npm",
# Node.js 安装路径
"$env:ProgramFiles\nodejs",
"${env:ProgramFiles(x86)}\nodejs",
# nvm 路径
"$env:NVM_HOME",
"$env:APPDATA\nvm",
# pnpm 路径
"$env:LOCALAPPDATA\pnpm",
"$env:APPDATA\pnpm",
# yarn 全局路径
"$env:LOCALAPPDATA\Yarn\bin",
# fnm 路径
"$env:LOCALAPPDATA\fnm_multishells",
# 用户 bin 目录
"$env:USERPROFILE\.local\bin",
"$env:USERPROFILE\bin"
)
$foundLocations = @()
foreach ($path in $searchPaths) {
if ([string]::IsNullOrEmpty($path)) { continue }
if (Test-Path $path) {
$claudeFile = Get-ChildItem -Path $path -Filter "claude.cmd" -ErrorAction SilentlyContinue
if ($claudeFile) {
$foundLocations += $claudeFile.FullName
Write-Host " [FOUND] $($claudeFile.FullName)" -ForegroundColor Green
}
# 也检查 claude (无扩展名,可能是 shim)
$claudeShim = Join-Path $path "claude"
if (Test-Path $claudeShim) {
$foundLocations += $claudeShim
Write-Host " [FOUND] $claudeShim" -ForegroundColor Green
}
}
}
# 全盘搜索 (可选,较慢)
Write-Host "`n 正在快速搜索常见位置..." -ForegroundColor Gray
$quickSearch = @(
"$env:APPDATA\npm\claude.cmd",
"$env:LOCALAPPDATA\pnpm\claude.cmd",
"$env:APPDATA\npm\node_modules\@anthropic-ai\claude-code\bin\claude.cmd"
)
foreach ($file in $quickSearch) {
if (Test-Path $file) {
if ($file -notin $foundLocations) {
$foundLocations += $file
Write-Host " [FOUND] $file" -ForegroundColor Green
}
}
}
if ($foundLocations.Count -eq 0) {
Write-Host " [WARN] 未找到 claude.cmd,可能未安装 Claude Code" -ForegroundColor Red
}
# 3. 检查 PATH 环境变量
Write-Host "`n[3] 分析 PATH 环境变量..." -ForegroundColor Yellow
$userPath = [Environment]::GetEnvironmentVariable("PATH", "User")
$machinePath = [Environment]::GetEnvironmentVariable("PATH", "Machine")
$currentPath = $env:PATH
Write-Host "`n 用户 PATH 条目:" -ForegroundColor Cyan
$userPathEntries = $userPath -split ";"
foreach ($entry in $userPathEntries) {
if ([string]::IsNullOrWhiteSpace($entry)) { continue }
$exists = Test-Path $entry
$status = if ($exists) { "[OK]" } else { "[MISSING]" }
$color = if ($exists) { "Gray" } else { "Red" }
Write-Host " $status $entry" -ForegroundColor $color
}
Write-Host "`n 系统 PATH 条目 (部分):" -ForegroundColor Cyan
$machinePathEntries = $machinePath -split ";"
$relevantMachine = $machinePathEntries | Where-Object {
$_ -match "node|npm|pnpm|yarn|nvm" -or $_ -match "Program Files"
} | Select-Object -First 10
foreach ($entry in $relevantMachine) {
if ([string]::IsNullOrWhiteSpace($entry)) { continue }
$exists = Test-Path $entry
$status = if ($exists) { "[OK]" } else { "[MISSING]" }
$color = if ($exists) { "Gray" } else { "Red" }
Write-Host " $status $entry" -ForegroundColor $color
}
# 4. 检查 npm 配置
Write-Host "`n[4] 检查 npm 配置..." -ForegroundColor Yellow
$npmCmd = Get-Command npm -ErrorAction SilentlyContinue
if ($npmCmd) {
Write-Host " npm 位置: $($npmCmd.Source)" -ForegroundColor Gray
try {
$npmPrefix = & npm config get prefix 2>$null
Write-Host " npm prefix: $npmPrefix" -ForegroundColor Gray
# 检查 npm prefix 是否在 PATH 中
if ($currentPath -like "*$npmPrefix*") {
Write-Host " [OK] npm prefix 在 PATH 中" -ForegroundColor Green
} else {
Write-Host " [FAIL] npm prefix 不在 PATH 中!" -ForegroundColor Red
Write-Host " 建议: 将 '$npmPrefix' 添加到 PATH" -ForegroundColor Yellow
}
} catch {
Write-Host " [WARN] 无法获取 npm prefix" -ForegroundColor Red
}
# 检查全局安装的包
Write-Host "`n 全局安装的 Claude 相关包:" -ForegroundColor Cyan
try {
$globalPackages = & npm list -g --depth=0 2>$null | Select-String -Pattern "claude|anthropic"
if ($globalPackages) {
foreach ($pkg in $globalPackages) {
Write-Host " $pkg" -ForegroundColor Gray
}
} else {
Write-Host " 未找到 Claude 相关全局包" -ForegroundColor Red
}
} catch {
Write-Host " 无法列出全局包" -ForegroundColor Red
}
} else {
Write-Host " [WARN] npm 未找到" -ForegroundColor Red
}
# 5. 检查 pnpm 配置
Write-Host "`n[5] 检查 pnpm 配置..." -ForegroundColor Yellow
$pnpmCmd = Get-Command pnpm -ErrorAction SilentlyContinue
if ($pnpmCmd) {
Write-Host " pnpm 位置: $($pnpmCmd.Source)" -ForegroundColor Gray
try {
$pnpmHome = & pnpm config get global-bin-dir 2>$null
if ($pnpmHome) {
Write-Host " pnpm global-bin-dir: $pnpmHome" -ForegroundColor Gray
if ($currentPath -like "*$pnpmHome*") {
Write-Host " [OK] pnpm bin 在 PATH 中" -ForegroundColor Green
} else {
Write-Host " [FAIL] pnpm bin 不在 PATH 中!" -ForegroundColor Red
Write-Host " 建议: 将 '$pnpmHome' 添加到 PATH" -ForegroundColor Yellow
}
}
} catch {
Write-Host " [INFO] 无法获取 pnpm 配置" -ForegroundColor Gray
}
} else {
Write-Host " [INFO] pnpm 未安装" -ForegroundColor Gray
}
# 6. 诊断总结
Write-Host "`n========================================" -ForegroundColor Cyan
Write-Host " 诊断总结" -ForegroundColor Cyan
Write-Host "========================================`n" -ForegroundColor Cyan
if ($claudeCmd) {
Write-Host "[OK] Claude Code 已正确配置" -ForegroundColor Green
Write-Host " 位置: $($claudeCmd.Source)" -ForegroundColor Gray
} elseif ($foundLocations.Count -gt 0) {
Write-Host "[PROBLEM] claude.cmd 已安装但不在 PATH 中" -ForegroundColor Red
Write-Host "`n找到的位置:" -ForegroundColor Yellow
foreach ($loc in $foundLocations) {
Write-Host " $loc" -ForegroundColor Gray
}
# 获取需要添加到 PATH 的目录
$dirsToAdd = $foundLocations | ForEach-Object { Split-Path $_ -Parent } | Select-Object -Unique
Write-Host "`n修复方法:" -ForegroundColor Yellow
Write-Host " 方法1: 临时添加到当前会话 PATH" -ForegroundColor Cyan
foreach ($dir in $dirsToAdd) {
Write-Host " `$env:PATH = `"$dir;`$env:PATH`"" -ForegroundColor White
}
Write-Host "`n 方法2: 永久添加到用户 PATH" -ForegroundColor Cyan
foreach ($dir in $dirsToAdd) {
Write-Host " [Environment]::SetEnvironmentVariable(`"PATH`", `"`$env:PATH;$dir`", `"User`")" -ForegroundColor White
}
Write-Host "`n 方法3: 通过系统设置添加" -ForegroundColor Cyan
Write-Host " 1. 按 Win+R,输入 sysdm.cpl" -ForegroundColor White
Write-Host " 2. 高级 -> 环境变量" -ForegroundColor White
Write-Host " 3. 在用户变量中编辑 PATH" -ForegroundColor White
Write-Host " 4. 添加: $($dirsToAdd -join '; ')" -ForegroundColor White
} else {
Write-Host "[PROBLEM] Claude Code 似乎未安装" -ForegroundColor Red
Write-Host "`n安装方法:" -ForegroundColor Yellow
Write-Host " npm install -g @anthropic-ai/claude-code" -ForegroundColor White
Write-Host " 或" -ForegroundColor Gray
Write-Host " pnpm add -g @anthropic-ai/claude-code" -ForegroundColor White
}
Write-Host "`n========================================`n" -ForegroundColor Cyan
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment