fnm管理不同项目nodejs版本

1,703 阅读3分钟

刚开始使用nvm进行nodejs版本管理,但是老旧项目同时维护,版本切来切去,十分麻烦。

Window 下安装 fnm

  • 设置全局默认版本
  • 为版本设置别名,以便随意调用
  • 为当前 shell 窗口临时切换 node 版本
  • 使用 .node-version 文件为项目配置默认版本以自动切换

安装 chocolatey

# 必须使用powershel  
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

安装 fnm

# 安装
choco install fnm
# 测试是否成功
fnm -h

环境变量配置

Powershell

%USERPROFILE%\Documents\WindowsPowerShell\profile.ps1  
  
# 文件内容  
fnm env --use-on-cd | Out-String | Invoke-Expression

%USERPROFILE%: 表示用户目录,直接在文件管理的地址栏输入 %USERPROFILE%,然后回车

WindowsPowerShell 为新建的目录, 如果安装 node 后命令仍然无法识别,将文件夹名称改为 PowerShell

cmd

  1. 搜索 cmd
  2. 打开文件所在位置
  3. 对 “命令提示符” 右键,点击属性
  4. 修改 目标 为下面的值
%windir%\system32\cmd.exe /k %USERPROFILE%\bashrc.cmd

5. 进入用户目录(%USERPROFILE%),添加文件 bashrc.cmd 6. 将下面的代码写入到上面的配置文件里面

@echo off
FOR /f "tokens=*" %%z IN ('fnm env --use-on-cd') DO CALL %%z

git bash

  1. linux 或者 mac 中
# 进入用户目录
cd ~

然后找到 .bash_profile 文件

  1. window 中

进入用户目录(%USERPROFILE%),新建文件 .bash_profile

  1. 将下面代码粘贴到 .bash_profile 中
eval $(fnm env | sed 1d)
export PATH=$(cygpath $FNM_MULTISHELL_PATH):$PATH

if [[ -f .node-version || -f .nvmrc ]]; then
   fnm use
fi

fnm 使用

> fnm -h
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
  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
  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, recursive]
      --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.node` field in `package.json` whenever a `.node-version` or `.nvmrc` file is not present.
          Experimental: This feature is subject to change.
          Note: `engines.node` can be any semver range, with the latest satisfying version being resolved. [env: FNM_RESOLVE_ENGINES]
  -h, --help
          Print help (see more with '--help')
  -V, --version
          Print version

安装 NodeJS

fnm install 16
fnm install 14
fnm install 12
# 默认从官方下载,如果慢,可以如下操作
fnm install 16 --node-dist-mirror=https://npmmirror.com/mirrors/node

使用 NodeJS

# 在当前控制台中切换nodejs 版本
fnm use 18
# 全局设置nodejs版本
fnm default 18

fnm 切换 node 默认版本

# 全局设置
fnm default 14

在特定项目中自动切换 nodejs 版本

当某个项目和默认node版本不一致,想持久化node版本,但是又不想影响全局node版本时,可以如下操作。 在特定项目根目录执行如下命令,或者手动创建.node-version文件,内容是nodejs版本号

echo '18' > .node-version

这样,当.node-version版本号和默认版本不一致时,会自动切换到当前版本

image.png image.png