---
title: "Fnm"
description: "fnm 的安装、配置、使用、常见问题等。"
canonical_url: "https://mhaibaraai.cn/docs/tools/version-control/fnm"
---
# Fnm

> fnm 的安装、配置、使用、常见问题等。

> [!NOTE]
> See: https://github.com/Schniz/fnm
> 
> Fast Node Manager (fnm) ： 一个快速的 Node.js 版本管理器，它可以帮助你在不同项目中切换 Node.js 版本。

## 安装

```sh [curl.sh]
curl -fsSL https://fnm.vercel.app/install | bash
```

```sh [brew.sh]
brew install fnm
```

## 配置环境

需要将 fnm 集成到你的 Shell（如 bash、zsh）。可以参考输出的安装脚本，或手动添加以下命令到你的 `.zshrc` 或 `.bashrc` 文件中：

```sh [sh]
eval "$(fnm env)"
source ~/.zshrc
```

> [!NOTE]
> 
> brew 在安装 fnm 后给出了环境配置的提示，并自动将 fnm 的路径和相关配置追加到 `~/.zshrc` 文件中
> 
> ```text
> ==> Running `brew cleanup fnm`...
> Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
> Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
> Installing for Zsh. Appending the following to /Users/yixuanmiao/.zshrc:
> 
> # fnm
> 
> FNM_PATH="/Users/yixuanmiao/Library/Application Support/fnm"
> if [ -d "$FNM_PATH" ]; then
> export PATH="/Users/yixuanmiao/Library/Application Support/fnm:$PATH"
> eval "`fnm env`"
> fi
> 
> In order to apply the changes, open a new terminal or run the following command:
> 
> source /Users/yixuanmiao/.zshrc
> ```

## 安装 Node.js

```sh [sh]
fnm install <version>
fnm use <version>
```

## 功能参数

- `--use-on-cd`：在每次进入目录时自动切换 Node.js 版本 （✅ 推荐）
- `--version-file-strategy=recursive`：递归查找 `.node-version` 或 `.nvmrc` 文件 （✅ 推荐）
- `--resolve-engines`：解析 `package.json` 中的 `engines.node` 字段 （🧪 实验）```json [package.json]
{
  "engines": {
    "node": ">=18.0.0"
  }
}
```
- `--corepack-enabled`： 使用 Corepack 作为包管理器 （🧪 实验）

## 常用命令

<table>
<thead>
  <tr>
    <th>
      命令
    </th>
    
    <th>
      功能说明
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        fnm ls-remote
      </code>
    </td>
    
    <td>
      查询所有 Node.js 版本
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        fnm install <version>
      </code>
    </td>
    
    <td>
      安装特定版本的 Node.js
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        fnm install --lts
      </code>
    </td>
    
    <td>
      安装最新的 LTS 版本
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        fnm use <version>
      </code>
    </td>
    
    <td>
      切换 Node.js 版本
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        fnm current
      </code>
    </td>
    
    <td>
      查看当前使用的 Node.js 版本
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        fnm default <version>
      </code>
    </td>
    
    <td>
      设置默认版本
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        fnm ls
      </code>
    </td>
    
    <td>
      查看所有已安装的 Node.js 版本
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        fnm uninstall <version>
      </code>
    </td>
    
    <td>
      卸载 Node.js
    </td>
  </tr>
</tbody>
</table>

## 报错处理

```vue
<template>
  <__flatten>
  <p>
  <strong>
  Q: zsh: command not found: node</strong></p>
  <blockquote>
  <p>
  [!WARNING]
  See: https://github.com/Schniz/fnm/issues/1279</p>
  <p>
  github issues : Zsh shell setup command did not work for me</p></blockquote></__flatten>
</template>
```

如果在使用 `node` 命令时出现 `zsh: command not found: node` 错误，可以尝试在 `.zshrc` 文件中替换以下配置：

```diff
FNM_PATH="/Users/yixuanmiao/Library/Application Support/fnm"
- if [ -d "$FNM_PATH" ]; then
export PATH="/Users/yixuanmiao/Library/Application Support/fnm:$PATH"
eval "`fnm env`"
- fi
```

:::

::

::


## Sitemap

See the full [sitemap](https://mhaibaraai.cn/sitemap.md) for all pages.
