截至2025年9月常用的npm源大全及详细的换源方法
-
目录
以下是截至2025年9月常用的npm源大全及详细的换源方法,涵盖了国内外多个可用源,不仅包括大厂提供的源,也包含一些特色镜像源:
一、国内外常用npm源列表
国内源(访问速度快)
-
淘宝 npm 镜像
https://registry.npmmirror.com
(原https://registry.npm.taobao.org
已更换为此域名,稳定可靠) -
npm 官方中国镜像
https://registry.npmjs.org.cn
(npm官方针对中国地区的镜像) -
华为云 npm 镜像
https://mirrors.huaweicloud.com/repository/npm/
(华为云提供的开源镜像,稳定性强) -
阿里云 npm 镜像
https://npm.aliyun.com
(阿里云开发者平台提供的镜像) -
腾讯云 npm 镜像
https://mirrors.cloud.tencent.com/npm/
(腾讯云开源镜像站) -
网易 npm 镜像
https://mirrors.163.com/npm/
(网易开源镜像,覆盖全面) -
字节跳动 npm 镜像
https://registry.bytedance.com
(字节内部源对外开放版本) -
清华大学 npm 镜像
https://mirrors.tuna.tsinghua.edu.cn/npm/
(高校镜像,学术环境常用) -
中科大 npm 镜像
https://mirrors.ustc.edu.cn/npm/
(中国科学技术大学镜像)
国外源(适合海外环境)
-
npm 官方源
https://registry.npmjs.org
-
Yarn 官方源
https://registry.yarnpkg.com
-
GitHub Packages
https://npm.pkg.github.com
(需登录GitHub账号,适合私有包) -
GitLab Packages
https://gitlab.com/api/v4/packages/npm/
-
Firebase 镜像
https://npm.pkg.dev/firebase
-
Cloudflare 镜像
https://registry.npmjs.cf
(Cloudflare加速的官方源镜像)
二、换源方法
1. 临时换源(单次安装使用)
安装包时通过
--registry
参数指定源:npm install 包名 --registry=https://registry.npmmirror.com
2. 永久换源(全局设置)
# 设置淘宝镜像 npm config set registry https://registry.npmmirror.com # 设置华为云镜像 npm config set registry https://mirrors.huaweicloud.com/repository/npm/ # 验证当前源 npm config get registry
3. 使用nrm工具管理多源(推荐)
nrm
是专门管理npm源的工具,可快速切换不同源:# 安装nrm npm install -g nrm # 查看内置源列表 nrm ls # 添加自定义源(例如添加字节跳动源) nrm add bytedance https://registry.bytedance.com # 切换到指定源 nrm use taobao # 切换到淘宝源 nrm use npm # 切换到官方源 # 测试源速度(对比各源响应时间) nrm test taobao
4. 项目级单独配置(局部源)
在项目根目录的
.npmrc
文件中设置(仅对当前项目生效):# .npmrc文件内容 registry=https://mirrors.tuna.tsinghua.edu.cn/npm/
三、注意事项
-
部分私有源(如GitHub Packages)需要先登录才能使用:
npm login --registry=https://npm.pkg.github.com
-
若源访问失败,可通过
ping
或nrm test
检查网络连通性。 -
切换源后,建议清除缓存避免冲突:
npm cache clean --force
-
企业内部通常有私有npm源(如Verdaccio、Nexus搭建),需咨询内部运维获取地址。
通过以上方法,可根据网络环境和需求灵活选择合适的npm源,提升包安装效率。
-