Private
Public Access
1
0

fix: npm install -g 后智能搜索全局 bin 路径避免命令找不到

npm install -g 成功后命令可能不在 PATH 中(尤其使用 fnm 管理的 Node.js)。
安装后通过 npm config get prefix 和 %APPDATA%\npm 查找全局 bin 目录
并自动加入当前会话 PATH。
This commit is contained in:
2026-03-20 23:47:40 +08:00
parent 9954678c4f
commit bb74d0f720
5 changed files with 66 additions and 0 deletions

View File

@@ -264,6 +264,20 @@ function Install-Tool {
return $false
}
# 如果命令不在 PATH 中,尝试查找 npm 全局 bin 目录
if (-not (Test-Command $Tool.Command)) {
Write-Info "正在搜索 npm 全局安装位置..."
$npmPrefix = & npm config get prefix 2>$null
if ($npmPrefix -and (Test-Path $npmPrefix)) {
$env:Path = "$npmPrefix;$env:Path"
}
# 也检查常见的 npm 全局路径
$appDataNpm = "$env:APPDATA\npm"
if ((Test-Path $appDataNpm) -and ($env:Path -notlike "*$appDataNpm*")) {
$env:Path = "$appDataNpm;$env:Path"
}
}
if (Test-Command $Tool.Command) {
Write-Success "$($Tool.Name) 安装成功!"
return $true