fnm 简介及使用

FNM 简介

一个快速、简单的 Node.js 版本管理工具,支持多版本切换、环境变量配置和镜像加速等功能。

github.com/Schniz/fnm

常用命令

命令 缩写 功能描述
​list-remote​ ​ls-remote​ 列出所有远程可安装的 Node.js 版本
​list​ ​ls​ 列出本地已安装的 Node.js 版本
​install​ ​i​ 安装指定版本的 Node.js
​use​ - 切换到指定版本(临时生效)
​default​ - 设置默认版本(永久生效)
​current​ - 显示当前使用的 Node.js 版本
​uninstall​ ​uni​ 卸载指定版本
​exec​ - 在 fnm 上下文中运行命令(如 fnm exec -- npm test​)
​env​ - 生成环境变量配置(需配合 eval​ 使用)
​alias​ - 给版本设置别名(如 fnm alias 18.0.0 latest​)
​unalias​ - 删除别名

常用选项

选项 说明
​--node-dist-mirror ​ 自定义 Node.js 下载镜像(默认:nodejs.org/dist​)
​--fnm-dir ​ 指定 fnm 安装根目录(通过环境变量 FNM_DIR​ 设置)
​--arch ​ 覆盖 Node 二进制文件的架构(如 x86​, arm64​)
​--log-level ​ 设置日志级别(可选:quiet​, error​, info​)
​--corepack-enabled​ 为每个新安装的 Node.js 启用 Corepack
​--resolve-engines​ 自动解析 package.json​ 中的 engines.node​ 版本(默认开启)

基础用法示例

  1. 安装指定版本:

    bash 复制代码
    fnm install 18.0.0     # 安装 18.0.0
    fnm i 20 --latest     # 安装 20.x 最新版本
  2. 版本切换:

    csharp 复制代码
    fnm use 18.0.0        # 临时切换到 18.0.0
    fnm default 20.0.0    # 设置默认版本
  3. 列出版本:

    bash 复制代码
    fnm list-remote       # 查看所有远程版本
    fnm ls                # 查看本地已安装版本
  4. 卸载版本:

    复制代码
    fnm uninstall 18.0.0
  5. 通过 package.json​ 自动切换版本(需启用 --resolve-engines​):

    bash 复制代码
    cd /project          # 项目目录包含 `package.json` 时自动切换
  6. 配置 Shell 环境(如 Bash/Zsh):

    bash 复制代码
    eval "$(fnm env)"    # 初始化 fnm 环境变量

高级配置

  1. 镜像加速(国内用户推荐):

    arduino 复制代码
    export FNM_NODE_DIST_MIRROR=https://npmmirror.com/mirrors/node/
  2. 自动切换目录版本:

    bash 复制代码
    # 在项目目录创建 `.node-version` 或 `.nvmrc` 文件
    echo "18.0.0" > .node-version
  3. 禁用 engines.node​ 解析:

    ini 复制代码
    fnm install --resolve-engines=false

其他命令

  • ​fnm completions​:生成 Shell 自动补全脚本。

  • ​fnm help ​:查看子命令帮助(如 fnm help install​)。

fnm help 翻译

sql 复制代码
A fast and simple Node.js manager

Usage: fnm [OPTIONS] <COMMAND>

Commands:
  list-remote  List all remote Node.js versions [aliases: ls-remote]
  list         List all locally installed Node.js versions [aliases: ls]
  install      Install a new Node.js version [aliases: i]
  use          Change Node.js version
  env          Print and set up required environment variables for fnm
  completions  Print shell completions to stdout
  alias        Alias a version to a common name
  unalias      Remove an alias definition
  default      Set a version as the default version
  current      Print the current Node.js version
  exec         Run a command within fnm context
  uninstall    Uninstall a Node.js version [aliases: uni]
  help         Print this message or the help of the given subcommand(s)

Options:
      --node-dist-mirror <NODE_DIST_MIRROR>
          <https://nodejs.org/dist/> mirror

          [env: FNM_NODE_DIST_MIRROR]
          [default: https://nodejs.org/dist]

      --fnm-dir <BASE_DIR>
          The root directory of fnm installations

          [env: FNM_DIR]

      --log-level <LOG_LEVEL>
          The log level of fnm commands

          [env: FNM_LOGLEVEL]
          [default: info]
          [possible values: quiet, error, info]

      --arch <ARCH>
          Override the architecture of the installed Node binary. Defaults to arch of fnm binary

          [env: FNM_ARCH]

      --version-file-strategy <VERSION_FILE_STRATEGY>
          A strategy for how to resolve the Node version. Used whenever `fnm use` or `fnm install` is called without a version, or when `--use-on-cd` is configured on evaluation

          [env: FNM_VERSION_FILE_STRATEGY]
          [default: local]

          Possible values:
          - local:     Use the local version of Node defined within the current directory
          - recursive: Use the version of Node defined within the current directory and all parent directories

      --corepack-enabled
          Enable corepack support for each new installation. This will make fnm call `corepack enable` on every Node.js installation. For more information about corepack see <https://nodejs.org/api/corepack.html>

          [env: FNM_COREPACK_ENABLED]

      --resolve-engines [<RESOLVE_ENGINES>]
          Resolve `engines.node` field in `package.json` whenever a `.node-version` or `.nvmrc` file is not present.
          This feature is enabled by default. To disable it, provide `--resolve-engines=false`.

          Note: `engines.node` can be any semver range, with the latest satisfying version being resolved.
          Note 2: If you disable it, please open an issue on GitHub describing _why_ you disabled it.
                  In the future, disabling it might be a no-op, so it's worth knowing any reason to
                  do that.

          [env: FNM_RESOLVE_ENGINES]
          [possible values: true, false]

  -h, --help
          Print help (see a summary with '-h')

  -V, --version
          Print version
ini 复制代码
一个快速简单的 Node.js 管理器

用法:fnm [选项] <命令>

命令:
  list-remote  列出所有远程的 Node.js 版本 [别名: ls-remote]
  list         列出所有本地安装的 Node.js 版本 [别名: ls]
  install      安装一个新的 Node.js 版本 [别名: i]
  use          更改 Node.js 版本
  env          打印并设置 fnm 所需的环境变量
  completions  打印 shell 补全到标准输出
  alias        将一个版本别名为一个常用名称
  unalias      删除一个别名定义
  default      设置一个版本为默认版本
  current      打印当前的 Node.js 版本
  exec         在 fnm 上下文中运行一个命令
  uninstall    卸载一个 Node.js 版本 [别名: uni]
  help         打印此消息或给定子命令的帮助信息

选项:
      --node-dist-mirror <NODE_DIST_MIRROR>
          <https://nodejs.org/dist/> 的镜像地址

          [环境变量: FNM_NODE_DIST_MIRROR]
          [默认值: https://nodejs.org/dist]

      --fnm-dir <BASE_DIR>
          fnm 安装的根目录

          [环境变量: FNM_DIR]

      --log-level <LOG_LEVEL>
          fnm 命令的日志级别

          [环境变量: FNM_LOGLEVEL]
          [默认值: info]
          [可能的值: quiet, error, info]

      --arch <ARCH>
          覆盖安装的 Node 二进制文件的架构。默认使用 fnm 二进制文件的架构

          [环境变量: FNM_ARCH]

      --version-file-strategy <VERSION_FILE_STRATEGY>
          用于解析 Node 版本的策略。每次调用 `fnm use` 或 `fnm install` 而没有指定版本,或者当 `--use-on-cd` 配置启用时使用。

          [环境变量: FNM_VERSION_FILE_STRATEGY]
          [默认值: local]

          可能的值:
          - local:     使用当前目录中定义的本地 Node 版本
          - recursive: 使用当前目录和所有父目录中定义的 Node 版本

      --corepack-enabled
          启用每个新安装的 corepack 支持。这将导致 fnm 在每次安装 Node.js 时调用 `corepack enable`。有关 corepack 的更多信息,请参见 <https://nodejs.org/api/corepack.html>

          [环境变量: FNM_COREPACK_ENABLED]

      --resolve-engines [<RESOLVE_ENGINES>]
          每当没有 `.node-version` 或 `.nvmrc` 文件时解析 `package.json` 中的 `engines.node` 字段。
          此功能默认启用。要禁用它,请提供 `--resolve-engines=false`。

          注意:`engines.node` 可以是任何 semver 范围,会解析最新满足的版本。
          注意 2:如果您禁用了它,请在 GitHub 上打开一个 issue 描述 _为什么_ 禁用了它。
                  未来,禁用它可能不会有任何效果,因此了解您这样做的原因是有价值的。

          [环境变量: FNM_RESOLVE_ENGINES]
          [可能的值: true, false]

  -h, --help
          打印帮助(使用 '-h' 查看摘要)

  -V, --version
          打印版本
相关推荐
星斗大森林2 小时前
flame游戏开发——地图拖拽与轻点判定(3)
前端
bug_kada2 小时前
玩转Flex布局:看完这篇你也是布局高手!
前端
前端小巷子2 小时前
JS打造“九宫格抽奖”
前端·javascript·面试
潘小安2 小时前
『译』资深前端开发者如何看待React架构
前端·react.js·面试
李昊哲小课3 小时前
HTML 完整教程与实践
前端·html
GISer_Jing3 小时前
React 18的createRoot与render全面对比
前端·react.js·前端框架
我叫汪枫3 小时前
React Hooks原理深度解析与高级应用模式
前端·react.js·前端框架
我叫汪枫3 小时前
深入探索React渲染原理与性能优化策略
前端·react.js·性能优化
阿智@113 小时前
推荐使用 pnpm 而不是 npm
前端·arcgis·npm