使用 nvm 安装 Node.js
1、安装 nvm
Windows 系统
正常下载地址:https://github.com/coreybutler/nvm-windows/
无法访问 GitHub 的解决方案:
方法一:使用国内镜像下载
# 从国内镜像站下载 nvm-windows
# 访问以下任一镜像站:
- https://mirrors.tuna.tsinghua.edu.cn/github-release/coreybutler/nvm-windows/
- https://mirrors.bfsu.edu.cn/github-release/coreybutler/nvm-windows/
方法二:使用 Gitee 镜像
# Gitee 上有 nvm-windows 的镜像仓库
https://gitee.com/mirrors/nvm-windows
安装步骤:
-
通过上述方法下载
nvm-setup.exe -
直接安装
-
验证安装:在 PowerShell 或终端运行
nvm
常见问题:
# 如果出现执行策略错误,以管理员身份运行:
Set-ExecutionPolicy RemoteSigned
# 然后按 a 确认
macOS 系统
正常安装命令:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash
无法访问 GitHub 的完整解决方案:
方法一:使用国内镜像安装(推荐)
# 使用 gitee 镜像(最稳定)
curl -o- https://gitee.com/mirrors/nvm/raw/master/install.sh | bash# 或者使用国内 CDN
curl -o- https://cdn.jsdelivr.net/gh/nvm-sh/nvm@v0.40.3/install.sh | bash
方法二:手动安装(最可靠)
# 1. 克隆国内镜像仓库
git clone https://gitee.com/mirrors/nvm.git ~/.nvm# 如果 Gitee 也访问不了,使用:
git clone https://hub.fastgit.xyz/nvm-sh/nvm.git ~/.nvm# 2. 切换到最新版本
cd ~/.nvm
git checkout v0.40.3# 3. 激活 nvm
source ~/.nvm/nvm.sh
方法三:修改 hosts 文件(临时解决)
# 在 /etc/hosts 文件中添加以下任一行:
199.232.68.133 raw.githubusercontent.com
185.199.108.133 raw.githubusercontent.com
185.199.109.133 raw.githubusercontent.com
185.199.110.133 raw.githubusercontent.com
185.199.111.133 raw.githubusercontent.com
安装后配置:
-
安装完成后重启命令行使环境变量生效
-
如果 nvm 命令不生效,在
~/.bashrc或~/.zshrc中添加:export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
2. 使用 nvm 安装 Node.js
查看可用版本
# Windows
nvm list available# macOS
nvm ls-remote
网络问题解决:如果查看版本列表失败,可以:
方法一:设置淘宝镜像(推荐)
# macOS/Linux 临时设置
export NVM_NODEJS_ORG_MIRROR=https://npmmirror.com/mirrors/node# 永久设置,添加到 ~/.bashrc 或 ~/.zshrc
echo 'export NVM_NODEJS_ORG_MIRROR=https://npmmirror.com/mirrors/node' >> ~/.zshrc# Windows 设置镜像
nvm node_mirror https://npmmirror.com/mirrors/node/
nvm npm_mirror https://npmmirror.com/mirrors/npm/
方法二:使用国内源直接下载
如果 nvm 安装仍然失败,可以直接从国内镜像下载 Node.js 二进制包:
# 访问淘宝 Node.js 镜像站手动下载
https://npmmirror.com/mirrors/node/# 然后使用 nvm 安装本地文件(macOS)
nvm install 22.19.0 --reinstall-packages-from=current --skip-npm
安装 Node.js(以 v22.19.0 LTS 为例)
# 安装指定版本
nvm install 22.19.0# Windows 需要额外运行(macOS 自动设置)
nvm use 22.19.0
安装缓慢的完整解决方案:
# 方法一:设置 node 镜像源(macOS/Linux)
export NVM_NODEJS_ORG_MIRROR=https://npmmirror.com/mirrors/node# 方法二:Windows 设置镜像
nvm node_mirror https://npmmirror.com/mirrors/node/
nvm npm_mirror https://npmmirror.com/mirrors/npm/# 方法三:使用代理(如果有)
export NVM_NODEJS_ORG_MIRROR=https://registry.npmmirror.com/node
验证安装
node -v
npm -v
# 应该显示安装的版本号
3. 切换 Node.js 版本
安装其他版本
nvm install 18.20.2
查看已安装版本
# Windows
nvm list# macOS
nvm ls
切换版本
nvm use 18.20.2# macOS(设置为默认版本)
nvm alias default 18.20.2
重要区别:
-
macOS:
nvm use是临时切换,重启后失效;nvm alias default是永久设置 -
Windows:
nvm use即可切换
4. 配置 npm 中国镜像
# 设置 npm 镜像(淘宝镜像)
npm config set registry https://registry.npmmirror.com/# 设置其他相关镜像
npm config set electron_mirror https://npmmirror.com/mirrors/electron/
npm config set sass_binary_site https://npmmirror.com/mirrors/node-sass/
npm config set phantomjs_cdnurl https://npmmirror.com/mirrors/phantomjs/
npm config set chromedriver_cdnurl https://npmmirror.com/mirrors/chromedriver/# 验证配置
npm config get registry
其他常用镜像:
# 腾讯云镜像
npm config set registry http://mirrors.cloud.tencent.com/npm/# 华为云镜像
npm config set registry https://mirrors.huaweicloud.com/repository/npm/# 清华大学镜像
npm config set registry https://mirrors.tuna.tsinghua.edu.cn/npm/
使用 cnpm(替代方案):
# 安装 cnpm
npm install -g cnpm --registry=https://registry.npmmirror.com# 使用 cnpm 代替 npm
cnpm install [package]
作用:加速包下载速度
5. 网络问题全面解决方案
GitHub 访问问题解决
方法一:使用 GitHub 镜像站
# 常用 GitHub 镜像站:
- https://hub.fastgit.xyz/
- https://github.com.cnpmjs.org/
- https://git.sdut.me/
- https://ghproxy.com/# 使用方法:将 github.com 替换为镜像域名
# 例如:https://github.com/user/repo 改为
# https://hub.fastgit.xyz/user/repo
方法二:使用 Gitee 导入
# 在 Gitee 上导入 GitHub 项目
# 1. 注册 Gitee 账号
# 2. 使用"从 GitHub/GitLab 导入仓库"功能
# 3. 克隆 Gitee 上的镜像仓库
方法三:修改 hosts 文件
# 获取最新的 GitHub IP 地址
# 访问:https://github.com.ipaddress.com/
# 或:https://fastly.net.ipaddress.com/github.global.ssl.fastly.net# 在 /etc/hosts 中添加:
140.82.113.4 github.com
199.232.69.194 github.global.ssl.fastly.net
方法四:使用 Git 配置代理
# 设置 Git 代理(如果自己有代理)
git config --global http.https://github.com.proxy http://127.0.0.1:7890
git config --global https.https://github.com.proxy https://127.0.0.1:7890
安装阶段问题
-
nvm 安装失败:使用国内镜像或手动安装
-
查看版本列表失败:设置
NVM_NODEJS_ORG_MIRROR环境变量 -
Node.js 下载缓慢:配置 nvm 镜像源
开发阶段问题
-
npm 包下载慢:配置 npm 镜像
-
原生模块编译问题:使用
--registry参数npm install --registry=https://registry.npmmirror.com
永久解决方案
将镜像配置写入 Shell 配置文件:
# 在 ~/.bashrc 或 ~/.zshrc 中添加:
export NVM_NODEJS_ORG_MIRROR=https://npmmirror.com/mirrors/node
export ELECTRON_MIRROR=https://npmmirror.com/mirrors/electron/
export SASS_BINARY_SITE=https://npmmirror.com/mirrors/node-sass/
alias cnpm="npm --registry=https://registry.npmmirror.com"
6. 国内开发者完整配置脚本
macOS/Linux 一键配置
#!/bin/bash
# 设置环境变量
echo 'export NVM_NODEJS_ORG_MIRROR=https://npmmirror.com/mirrors/node' >> ~/.zshrc
echo 'alias cnpm="npm --registry=https://registry.npmmirror.com"' >> ~/.zshrc# 安装 nvm(使用国内镜像)
curl -o- https://gitee.com/mirrors/nvm/raw/master/install.sh | bash# 重新加载配置
source ~/.zshrc# 安装 Node.js LTS 版本
nvm install --lts
nvm use --lts
nvm alias default node# 配置 npm
npm config set registry https://registry.npmmirror.com
Windows 配置脚本(PowerShell)
# 设置 nvm 镜像
nvm node_mirror https://npmmirror.com/mirrors/node/
nvm npm_mirror https://npmmirror.com/mirrors/npm/# 安装 Node.js
nvm install latest
nvm use latest# 配置 npm 镜像
npm config set registry https://registry.npmmirror.com
7. 课程总结
关键要点
-
安装方式:nvm 是最专业的 Node.js 安装方式
-
版本管理:轻松管理多个 Node.js 版本
-
版本选择:推荐安装 LTS(长期支持)版本
-
国内优化:配置 npm 中国镜像提升下载速度
-
网络问题:通过镜像源和替代方案解决无法访问 GitHub 的问题
核心命令回顾
-
nvm install [版本号]- 安装指定版本 -
nvm use [版本号]- 切换版本(Windows/临时) -
nvm alias default [版本号]- 设置默认版本(macOS) -
nvm list/ls- 查看已安装版本 -
node -v- 验证当前版本
国内开发环境配置清单
-
✅ 使用国内镜像安装 nvm
-
✅ 配置 nvm 的 Node.js 镜像源
-
✅ 设置 npm 中国镜像
-
✅ 配置 Shell 环境变量永久生效
-
✅ 了解 GitHub 替代访问方案
紧急情况备用方案
如果所有网络方法都失败:
-
使用手机热点(不同运营商网络可能不同)
-
前往本地的网吧或咖啡厅等公共场所
-
请有网络条件的朋友帮忙下载离线安装包
-
使用云服务器(国内厂商)进行安装,然后下载到本地
提示:在国内网络环境下,合理配置镜像源可以极大提升开发效率。建议收藏常用的镜像站地址,以备不时之需。如果条件允许,考虑使用稳定的科学上网工具作为长期解决方案。
