error: Can't find version in dotfiles. Please provide a version manually to the command.
zsh: command not found: node
fnm use 命令需要以下之一来确定使用哪个版本:
.node-version文件.nvmrc文件- 明确指定的版本号
如果项目中没有这些文件,fnm use 就会报错。
# 查看已安装的版本
fnm ls
# 使用指定版本
fnm use v24.14.1
# 设为默认版本
fnm default v24.14.1# 在项目目录下创建
echo "24" > .node-version
# 或指定完整版本
echo "24.14.1" > .node-version
# 然后 fnm use 就能自动识别
fnm use# 安装时创建别名
fnm install 24 --alias=default
# 使用别名
fnm use defaultfnm 安装了 Node.js,但没有正确配置到 Shell 环境中。
# 检查 fnm 命令
fnm --version
# 查看 fnm env 输出
fnm envZsh (~/.zshrc):
# 编辑配置文件
nano ~/.zshrc
# 添加以下内容(在文件末尾)
eval "$(fnm env --use-on-cd)"
# 保存后重新加载
source ~/.zshrcBash (~/.bashrc):
echo 'eval "$(fnm env)"' >> ~/.bashrc
source ~/.bashrcFish (~/.config/fish/config.fish):
fnm env --use-on-cd | source# 重新打开终端后
node -v
# 输出: v24.x.x
npm -v
# 输出: 10.x.x# 1. 安装 LTS 版本
fnm install --lts
# 2. 查看已安装的版本
fnm ls
# 输出类似:
# * v24.14.1
# system
# 3. 使用刚安装的版本(注意要指定版本号)
fnm use v24.14.1
# 4. 设为默认版本
fnm default v24.14.1
# 5. 验证
node -v# 方法 1: 获取第一个已安装版本
LTS_VERSION=$(fnm ls | grep -oE 'v[0-9]+\.[0-9]+\.[0-9]+' | head -1)
fnm use "$LTS_VERSION"
fnm default "$LTS_VERSION"
# 方法 2: 使用 fnm current(需要先有激活版本)
fnm use $(fnm ls-remote --lts | head -1 | awk '{print $1}')$ltsVersion = (fnm ls | Select-String -Pattern 'v[\d.]+' | Select-Object -First 1).Matches.Value
fnm use $ltsVersion
fnm default $ltsVersionfnm ls
# 输出:
# v18.20.0
# * v20.11.0 # * 表示当前使用的版本
# v24.14.1
# system# 临时切换(仅当前 Shell)
fnm use 18
# 设置默认版本(新终端使用)
fnm default 20
# 项目级版本(在项目目录创建 .node-version)
cd my-project
echo "18" > .node-version
# 进入目录自动切换(需要 --use-on-cd 配置)| 配置项 | 位置 |
|---|---|
| Node.js 版本 | ~/.local/share/fnm/node-versions/ |
| 别名 | ~/.local/share/fnm/aliases/ |
| Shell 配置 | ~/.zshrc / ~/.bashrc |
fnm install --lts # 安装最新 LTS
fnm install 20 # 安装 Node.js 20
fnm ls # 列出已安装版本
fnm ls-remote # 列出可安装版本
fnm use v20.11.0 # 使用指定版本
fnm default v20.11.0 # 设为默认
fnm current # 显示当前版本
fnm exec --using=18 -- node app.js # 使用指定版本执行命令
fnm uninstall 18 # 卸载版本