2026-04-14
教程
0

目录

一、Node.js 安装
1.1 Windows 系统
1.2 macOS 系统
方法一:官网安装
方法二:Homebrew 安装
二、npm 国内源配置
2.1 配置淘宝镜像(推荐)
2.2 安装并使用 cnpm
三、常用国内镜像源
四、常见问题解决
4.1 下载速度慢
4.2 安装包失败
五、总结

本文整理 Windows/macOS 安装 Node.js、配置 npm 国内镜像、使用 cnpm 及常见问题解决方案,用于提升包下载速度。

一、Node.js 安装

1.1 Windows 系统

  1. 访问 nodejs.org,下载 LTS 版本
  2. 双击安装包,按提示完成安装
  3. 验证安装
bash
node -v npm -v

1.2 macOS 系统

方法一:官网安装

  1. 下载 .pkg 安装包并双击安装
  2. 验证版本
bash
node -v npm -v

方法二:Homebrew 安装

bash
# 安装 Homebrew /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" # 安装 Node.js brew install node

二、npm 国内源配置

2.1 配置淘宝镜像(推荐)

  1. 临时使用
bash
npm --registry https://registry.npmmirror.com install 包名
  1. 永久配置
bash
npm config set registry https://registry.npmmirror.com # 验证 npm config get registry
  1. .npmrc 文件配置(推荐)
bash
echo "registry=https://registry.npmmirror.com" > .npmrc

2.2 安装并使用 cnpm

bash
# 全局安装 cnpm npm install -g cnpm --registry=https://registry.npmmirror.com # 使用 cnpm 安装包 cnpm install 包名 # 查看版本 cnpm -v

三、常用国内镜像源

  • 淘宝镜像:https://registry.npmmirror.com
  • 华为云镜像:https://mirrors.huaweicloud.com/repository/npm/
  • 腾讯云镜像:http://mirrors.cloud.tencent.com/npm/

四、常见问题解决

4.1 下载速度慢

  1. 确认已配置国内镜像
  2. 清除 npm 缓存
bash
npm cache clean -f

4.2 安装包失败

  1. 清除缓存
bash
npm cache clean -f
  1. 删除依赖与锁文件
bash
# macOS/Linux rm -rf node_modules package-lock.json # Windows rd /s /q node_modules del package-lock.json
  1. 重新安装
bash
npm install

五、总结

完成配置后可实现:

  • Windows/macOS 成功安装 Node.js
  • npm 永久切换国内镜像加速下载
  • 使用 cnpm 替代 npm 快速安装依赖
  • 解决下载慢、安装失败等常见问题

本文作者:苏皓明

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!