windows上使用fnm时遇到的问题

551 阅读1分钟

在使用fnm use时遇到错误

error: We can't find the necessary environment variables to replace the Node version.

仔细研究了一番,参考了几个文章后,发现大部分人都说的太复杂了。其实正确的方法在nodejs官方文档中就已经写出来了。这里贴一下。

使用包管理器下载nodejs

安装 fnm (快速 Node 管理器)

winget install Schniz.fnm

配置 fnm 环境

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

下载并安装 Node.js

fnm use --install-if-missing 22

验证环境中是否存在正确的 Node.js 版本

node -v # 应该打印 v22.11.0

验证环境中是否存在正确的 npm 版本

npm -v # # 应该打印 10.9.0

到这里我以为大功告成了,然后并没有,在执行fnm env的时候报错了,

bash: Out-String: command not found bash: Invoke-Expression: command not found thread 'main' panicked at library\std\src\io\stdio.rs:1118:9: failed printing to stdout: 管道正在被关闭。 (os error 232) note: run with RUST_BACKTRACE=1 environment variable to display a backtrace

仔细一看,官方文档下面有一行字,PowerShell。再看看我自己在哪写的,git bash。ok,打开PowerShell继续操作,大功告成,到git bash再试试fnm use也能正常使用了。

贴一份cmd,powershell和gitbash的区别的参考文章 Windows 命令提示符、PowerShell 和 Git Bash 的异同