在使用 Node.js 开发时,如果你希望提速 npm 包的下载(尤其是在我国国内),可以使用阿里云提供的 npm 镜像。这个镜像服务由阿里巴巴开源镜像站提供,官方地址是:
👉 https://npmmirror.com
原名为
cnpm镜像(registry.npm.taobao.org),现已升级为 npmmirror.com。
1. 使用阿里云 npm 镜像的方法
方法一:临时使用(单次命令)
npm install express --registry https://registry.npmmirror.com
方法二:设置全局镜像源
npm config set registry https://registry.npmmirror.com
设置完成后,所有 npm install 命令都会默认从阿里云镜像下载。
验证是否设置成功:
npm config get registry
# 输出应为:https://registry.npmmirror.com/
方法三:使用 npx 临时切换
npx -p node@18 node --version
# 自动从镜像下载包
2. 使用 cnpm(可选,不推荐新项目使用)
虽然你仍可安装 cnpm:
npm install -g cnpm --registry=https://registry.npmmirror.com
然后使用:
cnpm install express
但官方已不推荐广泛使用 cnpm,建议直接修改 registry 或使用 pnpm / yarn 配合镜像。
3. 配合 .npmrc 文件(推荐用于项目)
在项目根目录创建 .npmrc 文件,内容如下:
registry=https://registry.npmmirror.com
这样团队成员克隆项目后,会自动使用阿里云镜像。
4. 其他工具如何配置?
-
Yarn:
yarn config set registry https://registry.npmmirror.com -
pnpm:
pnpm config set registry https://registry.npmmirror.com
总结
| 工具 | 推荐镜像地址 |
|---|---|
| npm | https://registry.npmmirror.com |
| yarn | https://registry.npmmirror.com |
| pnpm | https://registry.npmmirror.com |
✅ 推荐做法:
将 registry 设置为 https://registry.npmmirror.com,无需额外工具,简单高效。
🔗 官方镜像站:https://developer.aliyun.com/mirror/npm
🔍 同步状态查看:https://npmmirror.com
如有其他 Node 版本管理需求(如 nvm),阿里云也有 Node.js 镜像,可通过设置 NVM_NODEJS_ORG_MIRROR 使用。
ECLOUD博客