使用 nvm 安装 Node.js

1、安装 nvm

Windows 系统

正常下载地址https://github.com/coreybutler/nvm-windows/

无法访问 GitHub 的解决方案

方法一:使用国内镜像下载
bash 复制代码
# 从国内镜像站下载 nvm-windows
# 访问以下任一镜像站:
- https://mirrors.tuna.tsinghua.edu.cn/github-release/coreybutler/nvm-windows/
- https://mirrors.bfsu.edu.cn/github-release/coreybutler/nvm-windows/
方法二:使用 Gitee 镜像
bash 复制代码
# Gitee 上有 nvm-windows 的镜像仓库
https://gitee.com/mirrors/nvm-windows

安装步骤

  1. 通过上述方法下载 nvm-setup.exe

  2. 直接安装

  3. 验证安装:在 PowerShell 或终端运行 nvm

常见问题

bash 复制代码
# 如果出现执行策略错误,以管理员身份运行:
Set-ExecutionPolicy RemoteSigned
# 然后按 a 确认

macOS 系统

正常安装命令

bash 复制代码
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash

无法访问 GitHub 的完整解决方案

方法一:使用国内镜像安装(推荐)
bash 复制代码
# 使用 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
方法二:手动安装(最可靠)
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 文件(临时解决)
bash 复制代码
# 在 /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 中添加:

    bash 复制代码
    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

查看可用版本

bash 复制代码
# Windows
nvm list available

# macOS
nvm ls-remote

网络问题解决:如果查看版本列表失败,可以:

方法一:设置淘宝镜像(推荐)
bash 复制代码
# 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 二进制包:

bash 复制代码
# 访问淘宝 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 为例)

bash 复制代码
# 安装指定版本
nvm install 22.19.0

# Windows 需要额外运行(macOS 自动设置)
nvm use 22.19.0

安装缓慢的完整解决方案

bash 复制代码
# 方法一:设置 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

验证安装

bash 复制代码
node -v
npm -v
# 应该显示安装的版本号

3. 切换 Node.js 版本

安装其他版本

bash 复制代码
nvm install 18.20.2

查看已安装版本

bash 复制代码
# Windows
nvm list

# macOS
nvm ls

切换版本

bash 复制代码
nvm use 18.20.2

# macOS(设置为默认版本)
nvm alias default 18.20.2

重要区别

  • macOS:nvm use 是临时切换,重启后失效;nvm alias default 是永久设置

  • Windows:nvm use 即可切换

4. 配置 npm 中国镜像

bash 复制代码
# 设置 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

其他常用镜像

bash 复制代码
# 腾讯云镜像
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(替代方案)

bash 复制代码
# 安装 cnpm
npm install -g cnpm --registry=https://registry.npmmirror.com

# 使用 cnpm 代替 npm
cnpm install [package]

作用:加速包下载速度

5. 网络问题全面解决方案

GitHub 访问问题解决

方法一:使用 GitHub 镜像站
bash 复制代码
# 常用 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 导入
bash 复制代码
# 在 Gitee 上导入 GitHub 项目
# 1. 注册 Gitee 账号
# 2. 使用"从 GitHub/GitLab 导入仓库"功能
# 3. 克隆 Gitee 上的镜像仓库
方法三:修改 hosts 文件
bash 复制代码
# 获取最新的 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 配置代理
bash 复制代码
# 设置 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

安装阶段问题

  1. nvm 安装失败:使用国内镜像或手动安装

  2. 查看版本列表失败 :设置 NVM_NODEJS_ORG_MIRROR 环境变量

  3. Node.js 下载缓慢:配置 nvm 镜像源

开发阶段问题

  1. npm 包下载慢:配置 npm 镜像

  2. 原生模块编译问题 :使用 --registry 参数

    bash 复制代码
    npm install --registry=https://registry.npmmirror.com

永久解决方案

将镜像配置写入 Shell 配置文件:

bash 复制代码
# 在 ~/.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 一键配置

bash 复制代码
#!/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)

bash 复制代码
# 设置 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. 课程总结

关键要点

  1. 安装方式:nvm 是最专业的 Node.js 安装方式

  2. 版本管理:轻松管理多个 Node.js 版本

  3. 版本选择:推荐安装 LTS(长期支持)版本

  4. 国内优化:配置 npm 中国镜像提升下载速度

  5. 网络问题:通过镜像源和替代方案解决无法访问 GitHub 的问题

核心命令回顾

  • nvm install [版本号] - 安装指定版本

  • nvm use [版本号] - 切换版本(Windows/临时)

  • nvm alias default [版本号] - 设置默认版本(macOS)

  • nvm list/ls - 查看已安装版本

  • node -v - 验证当前版本

国内开发环境配置清单

  1. ✅ 使用国内镜像安装 nvm

  2. ✅ 配置 nvm 的 Node.js 镜像源

  3. ✅ 设置 npm 中国镜像

  4. ✅ 配置 Shell 环境变量永久生效

  5. ✅ 了解 GitHub 替代访问方案

紧急情况备用方案

如果所有网络方法都失败:

  1. 使用手机热点(不同运营商网络可能不同)

  2. 前往本地的网吧或咖啡厅等公共场所

  3. 请有网络条件的朋友帮忙下载离线安装包

  4. 使用云服务器(国内厂商)进行安装,然后下载到本地


提示:在国内网络环境下,合理配置镜像源可以极大提升开发效率。建议收藏常用的镜像站地址,以备不时之需。如果条件允许,考虑使用稳定的科学上网工具作为长期解决方案。

相关推荐
labview_自动化3 小时前
Node.js
node.js
vvw&3 小时前
如何使用 Nodemon 自动重启 Node.js 应用
linux·运维·服务器·node.js
不说别的就是很菜3 小时前
【AI助手】从零构建文章抓取器 MCP(Node.js 版)
人工智能·node.js
网络精创大傻3 小时前
PHP 与 Node.js:实际性能对比
开发语言·node.js·php
BG8EQB3 小时前
Node.js 环境变量配置实战:从入门到精通
node.js
P7Dreamer10 小时前
package.json:你以为只是配置文件,其实是项目的命门!
前端·node.js
2503_9284115611 小时前
11.5 包和包管理器
数据库·arcgis·node.js·编辑器
中国lanwp17 小时前
全局 npm config 与多环境配置
前端·npm·node.js
你的人类朋友21 小时前
【Node】手动归还主线程控制权:解决 Node.js 阻塞的一个思路
前端·后端·node.js