当前位置: 首页 > news >正文

使用 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

安装步骤

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

  2. 直接安装

  3. 验证安装:在 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

安装阶段问题

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

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

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

开发阶段问题

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

  2. 原生模块编译问题:使用 --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. 课程总结

关键要点

  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. 使用云服务器(国内厂商)进行安装,然后下载到本地


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

http://www.dtcms.com/a/568834.html

相关文章:

  • Arbess零基础学习 - 使用Arbess+GitLab实现Node.js项目自动化构建/主机部署
  • Linux 虚拟化技术 KVM/ESXI/Docker
  • C006基于博途西门子1200PLC机械手分拣物料控制系统仿真
  • 网站建设ui设计dw怎么做别人可以看的网站
  • 毕业生就业网站开发项目禁用wordpress默认编辑器
  • 服务器数据恢复—raid5阵列硬盘离线搞崩溃,分区数据恢复案例来袭
  • 基于springboot的新闻资讯系统
  • H3C AC+AP本地转发二层组网
  • JavaEE 进阶第五期:Maven 之道,项目的依赖艺术与构建哲学
  • Linux:五种IO模型与非阻塞IO
  • unity hub在ubuntu 22.0.4上启动卡住
  • 自己做个网站需要什么网页设计尺寸用怎么量
  • 青建设厅官方网站申请域名后怎样建设网站
  • 安装Conda并配置PX4无人机仿真环境
  • 微信小程序全局配置分享功能
  • Spring Boot3零基础教程,StreamAPI 更多用法,笔记100
  • Unity UGC IDE实现深度解析(六):子图系统与模块化设计
  • 嵌入式Linux新手入门:北京迅为3568开发板驱动开发第二章helloworld 驱动实验
  • 动态背景网站邗江区做网站
  • Milvus:向现有Collections更改和添加字段(十一)
  • ESP32C3开发指南(基于IDF):console控制台命令行交互功能
  • harmonyos的鸿蒙的跳转页面的部署参数传递
  • Git 简介和基础使用
  • HarmonyOS Marquee组件深度解析:构建高性能滚动视觉效果
  • 网站建设绿茶科技zencart 网站建设
  • vscode arm交叉编译 中 cmakeTools 编译器设置
  • 全自动化立体仓库巷道堆垛机使用西门子1500PLC通过EtherCAT主站转Profinet实现与EtherCAT协议的伺服进行通讯方案案例
  • MAC M1系统用pyinstaller分别打包支持ARM系统的app和支持Inter系统的APP
  • MTK平台详解`adb devices`输出的序列号组成
  • uniapp实现的Tab 选项卡组件模板