Skip to content

FNM 快速开始

GitHub - Schniz/fnm: 🚀 Fast and simple Node.js version manager, built in Rust

安装与配置

安装

脚本安装 (macOS/Linux)

首先确保操作系统中已安装 curl 和 unzip ,然后执行:

shell
curl -fsSL https://fnm.vercel.app/install | bash
升级

在 macOS 上,实现起来就像 brew upgrade fnm 那样简单。

在其他操作系统上,升级 fnm 的过程与安装几乎相同。为防止 Shell 配置文件出现重复内容,请向安装命令传递 --skip-shell :

shell
curl -fsSL https://fnm.vercel.app/install | bash -s -- --skip-shell
参数

--install-dir

Set a custom directory for fnm to be installed. The default is $XDG_DATA_HOME/fnm (if $XDG_DATA_HOME is not defined it falls back to $HOME/.local/share/fnm on linux and $HOME/Library/Application Support/fnm on MacOS).
设置 fnm 安装的自定义目录。默认为 $XDG_DATA_HOME/fnm (如果未定义 $XDG_DATA_HOME ,则在 Linux 系统下回退为 $HOME/.local/share/fnm ,在 MacOS 系统下回退为 $HOME/Library/Application Support/fnm )。

--skip-shell

Skip appending shell specific loader to shell config file, based on the current user shell, defined in $SHELL. e.g. for Bash, $HOME/.bashrc$HOME/.zshrc for Zsh. For Fish - $HOME/.config/fish/conf.d/fnm.fish
跳过根据当前用户 Shell(定义在 $SHELL )向 Shell 配置文件添加特定加载器的步骤。例如对于 Bash 是 $HOME/.bashrc ,对于 Zsh 是 $HOME/.zshrc ,而对于 Fish 则是 $HOME/.config/fish/conf.d/fnm.fish 。

--force-install

macOS installations using the installation script are deprecated in favor of the Homebrew formula, but this forces the script to install using it anyway.
已弃用脚本安装的 macOS 安装方式,转而采用 Homebrew 配方,但仍强制使用脚本来完成安装。

Example:

shell
curl -fsSL https://fnm.vercel.app/install | bash -s -- --install-dir "./.fnm" --skip-shell

二进制安装 (Linux/macOS/Windows)

设置环境变量

Windows设置 > 系统 > 关于 > 高级系统设置 > 环境变量 > path 中新增一项

Linux 将 fnm 赋予可执行权限并移动到 /usr/local/bin 目录下,或者在其他任意地方然后配置环境变量 export $PATH:/path/to/fnm

Shell 设置

Bash

将以下内容添加到你的 .bashrc 配置文件:

bash
# ...

# 如果fnm可执行文件在 /usr/local/bin/ 文件夹中则无需配置环境变量否则需要配置:
# export $PATH:/usr/local/bin/fnm

if [ -f /usr/local/bin/fnm ]; then
  eval "$(fnm env --use-on-cd --version-file-strategy=recursive --shell bash)"
fi

设置 `--version-file-strategy=recursive` 会导致启动时报一个错误

PowerShell

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

ps1
fnm env --use-on-cd --version-file-strategy=recursive --shell powershell | Out-String | Invoke-Expression

Windows 系统下的路径位置位于:

  • %userprofile%\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 Powershell 5
  • %userprofile%\Documents\PowerShell\Microsoft.PowerShell_profile.ps1 Powershell 6+

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

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

若需编辑个人资料,请在 PowerShell 中运行以下命令:

shell
Invoke-Item $profile

镜像设置

Nodejs 镜像源

nodejs-release镜像_nodejs-release下载地址_nodejs-release安装教程-阿里巴巴开源镜像站 (aliyun.com)

nodejs-release | 镜像站使用帮助 | 清华大学开源软件镜像站 | Tsinghua Open Source Mirror

Nodejs Release 为各平台提供预编译的nodejs和npm等二进制文件,是https://nodejs.org/dist/ 的镜像。

# 设定环境变量
export FNM_NODE_DIST_MIRROR=https://mirrors.aliyun.com/nodejs-release/
export FNM_NODE_DIST_MIRROR=https://mirrors.tuna.tsinghua.edu.cn/nodejs-release/

下载存储路径

默认会下载nodejs到 C:\Users\wenjun\AppData\Roaming\fnm 文件夹,可修改环境变量来指定下载路径。

# 设定环境变量
FNM_DIR=C:\Users\wenjun\AppData\Roaming\fnm

fnm的一些常用命令

  • fnm install --latest:安装最新的Node.js版本
  • fnm install <version>:安装指定的Node.js版本
  • fnm use <version>:切换到已经安装的某个版本
  • fnm alias default <version>:设置默认的Node.js版本
  • fnm alias <version> <alias>:设置Node.js版本别名
  • fnm unalias <alias>:取消Node.js版本别名
  • fnm ls-remote:列出所有可用的Node.js版本
  • fnm ls:列出所有已经安装的Node.js版本
  • fnm uninstall <version>:卸载某个已经安装的Node.js版本
  • fnm env:查看当前环境变量