配置npm镜像源的几种方式

以配置淘宝镜像源为例 ——

全局配置

配置全局的 registry 属性

npm config set registry https://registry.npm.taobao.org

检查配置结果

npm config get registry  

测试一下(获取包信息)

npm info express  

使用 nrm 快速切换源(全局配置)

安装 nrm

npm install -g nrm

查看镜像源列表

nrm ls
* npm -------- https://registry.npmjs.org/
  yarn ------- https://registry.yarnpkg.com/
  cnpm ------- http://r.cnpmjs.org/
  taobao ----- https://registry.npm.taobao.org/
  nj --------- https://registry.nodejitsu.com/
  npmMirror -- https://skimdb.npmjs.com/registry/
  edunpm ----- http://registry.enpmjs.org/

切换到淘宝镜像源

nrm use taobao

再次查看镜像源列表,检查使用状态

  npm -------- https://registry.npmjs.org/
  yarn ------- https://registry.yarnpkg.com/
  cnpm ------- http://r.cnpmjs.org/
* taobao ----- https://registry.npm.taobao.org/
  nj --------- https://registry.nodejitsu.com/
  npmMirror -- https://skimdb.npmjs.com/registry/
  edunpm ----- http://registry.enpmjs.org/

项目级配置

在项目根目录下创建 .npmrc 文件进行配置

registry = https://registry.npm.taobao.org
  • 同样可以用 npm config get registrynpm info express 检查配置结果
  • 项目级的配置会覆盖全局配置

临时使用

安装包时通过 --registry 参数指定源

npm install express --registry https://registry.npm.taobao.org

使用 cnpm

也可以使用淘宝定制的 cnpm命令行工具代替默认的 npm

安装

npm install -g cnpm --registry=https://registry.npm.taobao.org

使用

cnpm install express

cnpm 支持 npm 除了 publish 之外的所有命令,如:

cnpm info express

关于 cnpm 的更多详情:
https://developer.aliyun.com/mirror/NPM?from=tnpm

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐