Node.JS 版本管理工具fnm 版本切换报错-Windows

1.报错提示
error: We can't find the necessary environment variables to replace the Node version. You should setup your shell profile to evaluate fnm env, see https://github.com/Schniz/fnm#shell-setup on how to do this Check out our documentation for more information: https://fnm.vercel.app
2.解决方案

PowerShell
将以下内容添加到配置文件的末尾:

fnm env --use-on-cd --shell powershell | Out-String | Invoke-Expression

对于 macOS/Linux,配置文件位于
~/.config/powershell/Microsoft.PowerShell_profile.ps1
对于 Windows,位置是:

%userprofile%\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1Powershell 5
%userprofile%\Documents\PowerShell\Microsoft.PowerShell_profile.ps1Powershell 6+

要创建配置文件,可以在 PowerShell 中运行以下命令:

if (-not (Test-Path $profile)) { New-Item $profile -Force }

编辑个人资料,请在 PowerShell 中运行以下命令:
Invoke-Item $profilenotepad $profile
也可以不放置推荐命令fnm env --use-on-cd --shell powershell | Out-String | Invoke-Expression 而放置以下命令

if (Get-Command fnm -ErrorAction SilentlyContinue) {
    $fnmOutputFile = "$env:TEMP\fnm_env.txt"

    Start-Process -NoNewWindow -FilePath "fnm" -ArgumentList 'env --use-on-cd --shell powershell' -RedirectStandardOutput $fnmOutputFile -Wait

    $envScript = Get-Content -Path $fnmOutputFile -Encoding UTF8 -Raw
    Invoke-Expression $envScript
}

Windows 命令提示符,又名批处理,又名 WinCMD
fnm 也受支持,但并未完全覆盖。您可以为cmd.exe或Windows 终端设置启动脚本,并附加以下几行:

@echo off
:: for /F will launch a new instance of cmd so we create a guard to prevent an infnite loop
if not defined FNM_AUTORUN_GUARD (
    set "FNM_AUTORUN_GUARD=AutorunGuard"
    FOR /f "tokens=*" %%z IN ('fnm env --use-on-cd') DO CALL %%z
)

3.参考信息:
fnm官方文档
stackoverflow

posted @ 2025-05-19 19:08  爱摸鱼的小贼  阅读(384)  评论(0)    收藏  举报