国内 npm 安装加速

2025-12-03
国内 npm 安装加速
思考过程
**Searching NPM Mirrors in China** I've begun the hunt for the current crop of dependable npm mirror sources in China. Taobao's mirror is a given, of course. I'm now looking into the Tencent and Huawei Cloud options and will assess their stability. Next, I plan to nail down the precise command syntax for both temporary and permanent registry changes. **Refining Mirror Discovery** My research has widened to encompass the 2024-2025 landscape of dependable npm mirrors in China. I'm focusing on validating the continued reliability of npmmirror and other options. I'm prioritizing accuracy for both command-line configuration and broader tool compatibility, which will all feed into the guide. **Investigating NPM Acceleration** I'm currently focused on the practical implications of accelerating npm installations in China, drawing from current search results. I'm prioritizing the established mirror sources, and initially it appears Taobao/Aliyun (npmmirror) is the frontrunner, due to the number of recommendations. Exploring the new addresses they provide, to ensure the information is up-to-date and relevant. **Reviewing Mirror Configurations** Now delving into the configuration techniques. I see clear distinctions between temporary usage with the `--registry` flag, and permanent global settings. Moreover, there's clear advice on how to verify the configuration and easily revert to the official registry. I'm noting the effectiveness of registry managers like `nrm` for ease of use. **Compiling a Concise Solution** My focus has shifted towards structuring the response. I'm prioritizing providing a direct solution using the npmmirror registry initially, followed by alternative options. I'm planning to detail each method, including how to configure them, verify the settings, and revert. I will include instructions for `nrm` and `cnpm` as options. I'm adding a section on troubleshooting, as the search results provided relevant tips, too. **Formulating a Comprehensive Response** I'm now refining the structure of the response, drawing on all gathered data. My focus is on creating a concise, easy-to-follow guide. I'll include the direct solution using `npmmirror`, along with alternate mirrors and configuration methods, including the use of `nrm` and `cnpm`. The response will conclude with troubleshooting steps. My aim is to make it as user-friendly as possible.
在国内加速 npm 安装,最推荐使用的是 **淘宝镜像(最新域名 npmmirror)** 或 **腾讯云镜像**。以下是 2025 年最新、最稳定的加速方案: ### 🚀 方案一:直接替换源(推荐,最简单) 这是最常用的方式,设置后永久生效,不用安装额外工具。 **1. 设置淘宝镜像(最新):** 淘宝原镜像域名(npm.taobao.org)已废弃,请务必使用下方的新域名: ```bash npm config set registry https://registry.npmmirror.com ``` **2. 或者设置腾讯云镜像:** 如果你所在网络对阿里云连接不佳,腾讯云也是极好的选择: ```bash npm config set registry https://mirrors.cloud.tencent.com/npm/ ``` **3. 验证是否生效:** ```bash npm config get registry # 如果返回上面的网址,说明设置成功 ``` --- ### 🛠 方案二:使用 `nrm` 管理源(最灵活) 如果你需要在公司私有源和国内公开源之间来回切换,推荐使用 `nrm` 工具。 1. **全局安装 nrm:** ```bash npm install -g nrm ``` 2. **查看可用源列表:** ```bash nrm ls ``` 3. **一键切换源:** ```bash nrm use taobao # 或者 nrm use tencent ``` 4. **测速(查看哪个最快):** ```bash nrm test ``` --- ### ⚡ 方案三:使用 `cnpm`(速度最快) `cnpm` 是淘宝团队定制的命令行工具,专门用于加速安装,它的安装速度通常比直接修改 npm 源更快,因为它支持并发下载。 1. **安装 cnpm:** ```bash npm install -g cnpm --registry=https://registry.npmmirror.com ``` 2. **使用 cnpm 安装包:** 以后安装包时,直接把 `npm` 命令换成 `cnpm` 即可: ```bash cnpm install axios ``` --- ### 📝 临时使用(仅一次) 如果你不想修改全局配置,只想在安装某个包时临时加速: ```bash npm install 你的包名 --registry=https://registry.npmmirror.com ``` ### ⚠️ 常见问题 * **报错证书过期/连接失败?** 如果你还在使用旧的 `registry.npm.taobao.org`,请立即按照方案一更新为 `registry.npmmirror.com`。 * **如何恢复官方源?** 如果需要发布包到 npm 官方库,可以切换回官方源: ```bash npm config set registry https://registry.npmjs.org/ ```