删库在逃程序员的Blog

NVM 完全指南:Node.js 版本管理的终极武器

author
·
43
0

NVM 完全指南:Node.js 版本管理的终极武器

什么是 NVM?

NVM (Node Version Manager) 是一个用于管理多个 Node.js 版本的命令行工具。它允许你在同一台机器上快速安装、切换和使用不同版本的 Node.js,而无需手动下载、配置或处理权限问题。

为什么需要 NVM?

  1. 项目兼容性 - 不同项目可能依赖不同版本的 Node.js
  2. 测试需求 - 需要测试代码在多个 Node 版本下的表现
  3. 避免权限问题 - 无需 sudo 即可全局安装 npm 包
  4. 快速切换 - 一行命令即可切换 Node 版本

安装 NVM

Linux / macOS 一键安装

# 使用 curl
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.4/install.sh | bash

# 或使用 wget
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.4/install.sh | bash

Windows 安装(nvm-windows)

⚠️ 注意: Windows 需要使用 nvm-windows 版本,与 Linux/macOS 的 nvm 不同。

下载地址:

安装步骤:

  1. 卸载旧版 Node.js(如已安装)- 避免冲突
  2. 运行安装程序 - 选择同意协议
  3. 选择安装目录 - 建议 D:\nvm(不要带中文路径)
  4. 选择 Node 安装目录 - 建议 D:\nvm\nodejs
  5. 取消邮件订阅 - 全部取消勾选
  6. 完成安装 - 打开 PowerShell 验证

验证安装:

# PowerShell 或 CMD
nvm -v

输出版本号表示安装成功。

常见问题:

  • ERROR: open \settings.txt - 关闭并重新打开 PowerShell/CMD
  • 命令找不到 - 检查环境变量 NVM_HOMENVM_SYMLINK

nvm-windows 常用命令:

# 安装 Node.js
nvm install 20.11.0

# 切换到指定版本
nvm use 20.11.0

# 列出已安装版本
nvm list

# 设置镜像源(国内必备)
nvm node_mirror https://npmmirror.com/mirrors/node/
nvm npm_mirror https://npmmirror.com/mirrors/npm/

国内用户推荐:使用国内镜像安装

由于 GitHub raw 文件在国内访问较慢,可以使用国内镜像源:

# 方式一:使用国内镜像脚本
export NVM_NODEJS_ORG_MIRROR=https://npmmirror.com/mirrors/node/
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.4/install.sh | bash

# 方式二:手动克隆(更稳定)
git clone https://gitee.com/RubyMetric/nvm.git ~/.nvm

验证安装

command -v nvm

输出 nvm 表示安装成功。

手动配置(如果自动配置失败)

将以下内容添加到你的 shell 配置文件(~/.bashrc~/.zshrc~/.profile):

export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # 加载 nvm

国内配置(必读!)

配置镜像源加速下载

国内访问官方源速度慢,建议配置以下镜像:

# 临时设置(当前终端会话)
export NVM_NODEJS_ORG_MIRROR=https://npmmirror.com/mirrors/node/
export NVM_IOJS_ORG_MIRROR=https://npmmirror.com/mirrors/iojs/

# 永久设置(推荐)
echo 'export NVM_NODEJS_ORG_MIRROR=https://npmmirror.com/mirrors/node/' >> ~/.zshrc
echo 'export NVM_IOJS_ORG_MIRROR=https://npmmirror.com/mirrors/iojs/' >> ~/.zshrc
source ~/.zshrc

配置 npm 镜像

# 设置 npm 镜像为淘宝源
npm config set registry https://registry.npmmirror.com

# 永久写入 ~/.npmrc
echo 'registry=https://registry.npmmirror.com' >> ~/.npmrc

一键配置脚本

创建 ~/.nvm_config.sh 脚本:

#!/bin/bash
# NVM 国内配置一键脚本

# 配置 NVM 镜像
export NVM_NODEJS_ORG_MIRROR=https://npmmirror.com/mirrors/node/
export NVM_IOJS_ORG_MIRROR=https://npmmirror.com/mirrors/iojs/

# 添加到 shell 配置
cat >> ~/.zshrc << 'EOF'

# NVM 国内镜像配置
export NVM_NODEJS_ORG_MIRROR=https://npmmirror.com/mirrors/node/
export NVM_IOJS_ORG_MIRROR=https://npmmirror.com/mirrors/iojs/
EOF

# 配置 npm 镜像
npm config set registry https://registry.npmmirror.com

echo "✅ NVM 国内配置完成!"

核心命令速查

安装 Node.js

# 安装最新版本
nvm install node

# 安装指定版本
nvm install 20
nvm install 18.17.0

# 安装 LTS 版本
nvm install --lts

# 安装时设置默认
nvm install 20 --default

切换版本

# 切换到指定版本
nvm use 20

# 使用已安装的某个版本
nvm use 18.17.0

查看版本

# 列出所有已安装的版本
nvm ls

# 列出所有可安装的版本
nvm ls-remote

# 查看当前使用的版本
nvm current

设置默认版本

# 设置默认 Node 版本(新终端自动使用)
nvm alias default 20

卸载版本

# 卸载指定版本
nvm uninstall 16

高级技巧

1. 使用 .nvmrc 文件

在项目根目录创建 .nvmrc 文件,指定项目所需的 Node 版本:

# .nvmrc 内容
20.11.0

进入项目目录后自动切换:

nvm use  # 自动读取 .nvmrc 并切换版本

2. 迁移全局包

安装新版本时迁移全局 npm 包:

nvm install 20 --reinstall-packages-from=18

3. 清理旧版本

# 卸载除当前版本外的所有版本
nvm uninstall --lts

# 卸载指定版本
nvm uninstall 14

4. 运行命令时使用特定版本

# 临时使用某个版本运行命令
nvm exec 18 node app.js

5. 查看版本使用路径

# 查看当前 Node 的安装路径
nvm which current

# 查看指定版本的路径
nvm which 20

6. 别名管理

# 创建版本别名
nvm alias stable 20.11.0
nvm alias myproject 18.17.0

# 使用别名
nvm use stable
nvm use myproject

# 删除别名
nvm alias default node

Docker 中使用 NVM

FROM ubuntu:latest
ARG NODE_VERSION=20

# 安装 curl
RUN apt update && apt install curl -y

# 配置国内镜像
ENV NVM_NODEJS_ORG_MIRROR=https://npmmirror.com/mirrors/node/

# 安装 nvm
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.4/install.sh | bash

# 设置环境变量
ENV NVM_DIR=/root/.nvm

# 安装 Node.js
RUN bash -c "source $NVM_DIR/nvm.sh && nvm install $NODE_VERSION"

# 设置 ENTRYPOINT
ENTRYPOINT ["bash", "-c", "source $NVM_DIR/nvm.sh && exec "$@"", "--"]

CMD ["/bin/bash"]

常见问题排查

macOS 安装失败

  1. 确保安装了 Xcode Command Line Tools:

    xcode-select --install
  2. 如果使用 zsh,确保 ~/.zshrc 存在:

    touch ~/.zshrc

Linux 命令找不到

# 重新加载配置文件
source ~/.bashrc  # 或 source ~/.zshrc

# 或重启终端

Windows 常见问题

  • settings.txt 错误 - 关闭并重新打开 PowerShell
  • 命令找不到 - 检查环境变量配置
  • 权限问题 - 以管理员身份运行 PowerShell

权限问题

使用 nvm 后,全局安装 npm 包不再需要 sudo:

# ✅ 正确
npm install -g pm2

# ❌ 不需要
sudo npm install -g pm2

国内下载速度慢

确保已配置镜像源:

# 检查当前镜像配置
echo $NVM_NODEJS_ORG_MIRROR

# 应输出:https://npmmirror.com/mirrors/node/

最佳实践建议

  1. 每个项目使用 .nvmrc - 确保团队成员使用相同的 Node 版本
  2. 定期清理旧版本 - 避免磁盘空间浪费
  3. 使用 LTS 版本 - 生产环境优先选择长期支持版本
  4. 配置镜像 - 国内用户务必配置镜像源加速下载
  5. 备份全局包列表 - 重装系统前导出全局包列表
  6. 团队协作统一版本 - 在 README 中注明所需 Node 版本

常用 Node.js 版本参考

版本 代号 LTS 停止维护
24.x - -
22.x Iron 2027-04
20.x Hydrogen 2026-04
18.x Hydrogen 2025-04
16.x Gallium 2024-09

推荐: 生产环境使用 20.x22.x LTS 版本


资源链接


总结

NVM 是每个 Node.js 开发者必备的工具。它让版本管理变得简单、安全、高效。花 10 分钟安装配置,未来能节省无数小时的排错时间。

立即行动:

# 1. 安装 NVM
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.4/install.sh | bash

# 2. 配置国内镜像(重要!)
echo 'export NVM_NODEJS_ORG_MIRROR=https://npmmirror.com/mirrors/node/' >> ~/.zshrc
source ~/.zshrc

# 3. 安装 Node.js
nvm install 20
nvm use 20

# 4. 验证
node -v
npm -v

最后更新:2026 年 4 月

评论 (0)