NVM / FNM 安装使用

2026 更新:不推荐使用 nvm,推荐使用 fnm
文章已修改。

安装

Linux / macOS

1
curl -o- https://fnm.vercel.app/install | bash

也可以用 Homebrew 安装:

1
brew install fnm

Windows

Windows 这边可以用 WinGet 安装,也可以用 Scoop 安装:

1
2
winget install Schniz.fnm
# scoop install fnm

配置终端

Bash / Zsh

~/.bashrc~/.zshrc 中添加:

1
2
eval "$(fnm env --use-on-cd --shell bash)"
# eval "$(fnm env --use-on-cd --shell zsh)"

PowerShell

PowerShell 分为 Windows PowerShell 和 PowerShell Core,两个的配置文件路径不一样,需要分别配置(如果你两个都要用的话)。

查看配置地址:

1
2
Write-Host $PROFILE
if (-not (Test-Path $PROFILE)) { New-Item $PROFILE -Force }

$PROFILE 中添加:

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

CMD

(独家)

如果你使用 CMD,那么大概率不支持 FNM 的自动切换功能。但如果你需要在脚本中使用,可以这么写:

1
2
3
4
5
6
@echo off
fnm env --shell cmd >> %TEMP%\fnm-env.cmd
call %TEMP%\fnm-env.cmd
del /q %TEMP%\fnm-env.cmd

node -v

安装 Node

fnm 默认从官方服务器下载 Node.js。在中国大陆环境下,建议配置镜像以加速下载。

  • 永久生效:设置环境变量 FNM_NODE_DIST_MIRROR
1
2
export FNM_NODE_DIST_MIRROR="https://npmmirror.com/mirrors/node/"
# PowerShell: $env:FNM_NODE_DIST_MIRROR="https://npmmirror.com/mirrors/node/"
1
fnm install 24
  • 临时生效:
1
fnm install 24 --node-dist-mirror="https://npmmirror.com/mirrors/node/"

安装 PNPM

在安装 pnpm 前,需要换一下 NPM 源:

1
npm config --global set registry https://registry.npmmirror.com

或者:

1
2
3
4
export COREPACK_NPM_REGISTRY="https://registry.npmmirror.com"
export NPM_CONFIG_REGISTRY="https://registry.npmmirror.com"
# PowerShell: $env:COREPACK_NPM_REGISTRY="https://registry.npmmirror.com"
# PowerShell: $env:NPM_CONFIG_REGISTRY="https://registry.npmmirror.com"

然后:

1
2
3
corepack enable pnpm

pnpm -v

切换版本

1
fnm use 20

设置默认版本

1
fnm default 20