macOS上管理多个Node.js版本fnm

管理工具 fnmnvm

nvm:作为最广泛使用的 Node.js 版本管理器,使用 Bash 编写,适用于类 UNIX 环境(如 macOS 和 Linux),也可以通过兼容的 shell(如 WSL)在 Windows 上使用。
fnm:(Fast Node Manager)一种较新的、快速的替代方案,由 Rust 编写,以其速度闻名。支持多线程,因此在安装和切换 Node 版本时特别快。它还提供跨平台支持,包括 WSL、macOS 和 Windows。
这里以 fnm 为例。

下载安装Homebrew(如果尚未安装)

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

更新
brew update

安装 fnm

brew install fnm
echo 'eval "$(fnm env --use-on-cd)"' >> ~/.zshrc
source ~/.zshrc

使用 fnm 安装 Node.js

# 安装最新 LTS 版本
fnm install --lts

# 安装特定版本(例如 Node 18)
fnm install 18

# 安装最新版本
fnm install latest

切换 Node.js 版本

# 使用全局版本
fnm use --global 18

# 为当前目录设置本地版本(会创建 .node-version 文件)
fnm use 20

验证安装

# 查看当前使用的 Node 版本
node -v

# 查看已安装的所有版本
fnm list

其他常用命令

# 列出官方可用版本
fnm ls-remote

# 删除特定版本
fnm uninstall 16

# 设置默认版本
fnm default 18
posted @ 2025-05-02 11:12  Ritchie^._.^  阅读(219)  评论(0)    收藏  举报