推薦 3 款實(shí)用 Node.js 版本管理工具

為了能夠?qū)?Node.js 版本進(jìn)行版本管理,我整理了 3 款非常實(shí)用的 Node.js 版本管理工具,讓大家能夠自由地切換本地環(huán)境不同的 Node.js 版本。
1. nvm
? Github stars: 60K+
nvm[2] 是一款 Node.js 版本管理工具,允許用戶通過命令行快速安裝、切換和管理不同的 Node.js 版本。

(圖片來自:github[3])
nvm 只適用于 macOS 和 Linux 用戶的項(xiàng)目,如果是 Windows 用戶,可以使用 nvm-windows[4] 、nodist[5] 或 nvs[6] 替換。
安裝方式
macOS 下載方式:
# 方式1 瀏覽器打開下面鏈接下載
https://github.com/nvm-sh/nvm/blob/v0.39.1/install.sh
# 下載完成后,通過命令安裝
sh install.sh
# 方式2 推薦
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
# 方式3
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
安裝過程中如果遇到一些奇怪的問題,可以查看下 nvm 補(bǔ)充說明[7]。
常用命令
nvm ls # 查看版本安裝所有版本
nvm ls-remote # 查看遠(yuǎn)程所有的 Node.js 版本
nvm install 17.0.0 # 安裝指定的 Node.js 版本
nvm use 17.0.0 # 使用指定的 Node.js 版本
nvm alias default 17.0.0 # 設(shè)置默認(rèn) Node.js 版本
nvm alias dev 17.0.0 # 設(shè)置指定版本的別名,如將 17.0.0 版本別名設(shè)置為 dev

2. n
? Github stars: 16.7K+
n[8] 是一款交互式的 Node.js 版本管理工具,沒有子腳本,沒有配置文件,也沒有復(fù)雜的 API,使用起來非常簡單。
n 只適用于 macOS 和 Linux ,不適用于 Windows。
安裝方式
可以使用 npm 直接安裝到全局:
npm install n -g
常用命令
n # 顯示所有已下載版本
n 10.16.0 # 下載指定版本
n lts # 查看遠(yuǎn)程所有 LTS Node.js 版本
n run 10.16.0 # 運(yùn)行指定的 Node.js 版本
輸入 n -h查看幫助信息,主要命令如下:
n Display downloaded Node.js versions and install selection
n latest Install the latest Node.js release (downloading if necessary)
n lts Install the latest LTS Node.js release (downloading if necessary)
n <version> Install Node.js <version> (downloading if necessary)
n install <version> Install Node.js <version> (downloading if necessary)
n run <version> [args ...] Execute downloaded Node.js <version> with [args ...]
n which <version> Output path for downloaded node <version>
n exec <vers> <cmd> [args...] Execute command with modified PATH, so downloaded node <version> and npm first
n rm <version ...> Remove the given downloaded version(s)
n prune Remove all downloaded versions except the installed version
n --latest Output the latest Node.js version available
n --lts Output the latest LTS Node.js version available
n ls Output downloaded versions
n ls-remote [version] Output matching versions available for download
n uninstall Remove the installed Node.js
3. fnm
? Github stars: 8.4K+
fnm [9]是一款快速簡單 ?? 的 Node.js 版本管理器,使用 Rust 構(gòu)建。

(圖片來自:freecodecamp[10])
主要特點(diǎn)包括:
?? 跨平臺支持,包括:macOS、Windows、Linux; ? 單一文件,輕松安裝,即時(shí)啟動(dòng) ; ?? 以速度為設(shè)計(jì)理念; ?? 適用于 .node-version和.nvmrc文件;
安裝方式
macOS / Linux 環(huán)境:
# bash, zsh and fish shells
curl -fsSL https://fnm.vercel.app/install | bash
Windows 環(huán)境:
# 管理員模式打開終端,安裝后只能使用管理員模式打開使用
choco install fnm
# 安裝完成還需要手動(dòng)設(shè)置環(huán)境變量
Linux/macOS/Windows 環(huán)境也可以直接下載二進(jìn)制文件安裝,下載地址:https://github.com/Schniz/fnm/releases[11]
常用命令
fnm -h # 查看幫助
fnm install 17.0.0 # 安裝指定 Node.js 版本
fnm use 17.0.0 # 使用指定 Node.js 版本
fnm default 17.0.0 # 設(shè)置默認(rèn) Node.js 版本
總結(jié)
本文為大家推薦了 3 款非常常用的 Node.js 版本管理工具,大家可以按照自己實(shí)際需求選擇使用。
如果本文給您帶來幫助,還請點(diǎn)贊??支持一下啦。
