Skip to Content
前端Miscnode 版本管理

使用 fnm 管理 Node.js 版本

推荐优先使用 fnm,速度比 nvm 快。

zsh 配置

fnm env 的初始化放在 统一 zsh 配置 的可选插槽里。独立配置时可以使用:

eval "$(fnm env --use-on-cd --shell zsh)"

WSL PATH 问题

如果 WSL shell 会继承旧 session 或 Windows 侧注入的 Node 路径,可能在切到 fnm 后仍然误用 旧的 ~/.nvm 路径,或残留的 fnm_multishells 路径。fnm env --use-on-cd 会在当前 shell 里 正常建立 multishell,但它挡不住父 shell 继承来的旧 multishell PATH。

排查时重点看 PATH 里是否残留这些模式:

*/.nvm/* */fnm_multishells/*

Windows Terminal PowerShell 配置

Add the following to the end of your profile file:

fnm env --use-on-cd --shell powershell | Out-String | Invoke-Expression
# if the profile file does not exist, create it if (-not (Test-Path $PROFILE)) { New-Item $PROFILE -Force } # open the profile file in notepad notepad $PROFILE

常用命令

fnm install --lts fnm default lts-latest fnm use lts-latest fnm current fnm list
  • fnm install --lts:安装最新 LTS 版本。
  • fnm install [version]:安装指定 Node 版本,例如 22lts-latest
  • fnm use [version]:切换当前 shell 的 Node 版本。
  • fnm default [version]:设置默认 Node 版本。
  • fnm current:查看当前 shell 正在使用的 Node 版本。
  • fnm list:查看本机已安装的 Node 版本。

nvm

常用命令

  • nvm arch:显示 Node 是运行在 32 位还是 64 位。
  • nvm install [arch]:安装 Node,arch 可省略,也可以指定 32 位或 64 位。
  • nvm list [available]:显示已安装的列表;加 available 可以看可安装版本。
  • nvm on:开启 Node 版本管理。
  • nvm off:关闭 Node 版本管理。
  • nvm proxy [url]:设置下载代理,none 可移除代理。
  • nvm node_mirror [url]:设置 Node 镜像。
  • nvm npm_mirror [url]:设置 npm 镜像。
  • nvm uninstall [version]:卸载指定版本 Node。
  • nvm use [version] [arch]:切换到指定版本 Node,可指定 32/64 位。
  • nvm root [path]:设置存储不同版本 Node 的目录。
  • nvm version:显示 nvm 版本。

安装

# 安装最新的 LTS 版本 nvm install lts # 安装指定版本 nvm install 20 # 安装最新版本 nvm install latest

切换

# 列出所有已安装的版本 nvm list # 切换到指定版本 nvm use 20 # 切换到最新的 LTS 版本 nvm use lts # 切换到最新版本 nvm use latest

使用 nrm 切换 npm 源

# 列出所有源 npx nrm ls # 切换到淘宝源 npx nrm use taobao # 切换到 npm 官方源 npx nrm use npm # 切换到 yarn 源 npx nrm use yarn # 切换到 tencent 源 npx nrm use tencent
Last updated on