修改npm和yarn源

作者: 梧桐叶_ | 来源:发表于2018-10-19 11:36 被阅读151次

作为前端开发者我们经常会使用npm或yarn,我们都知道npm和yarn默认镜像在国外,国内访问这些镜像速度会比较慢,下面介绍修改npm和yarn源的方法

一. npm和yarn源的简单修改(以淘宝镜像为例)

npm

1. 临时修改(只生效一次)
npm install 包的名字 --registry https://registry.npm.taobao.org
2. 设置npm的配置项(全局配置)

查看npm源的当前地址

npm config get registry

设置淘宝镜像

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

yarn

1. 临时修改(只生效一次)
yarn save 包的名字 --registry https://registry.npm.taobao.org/
2. 设置yarn的配置项(全局配置)

查看yarn源的当前地址

yarn config get registry

设置淘宝镜像

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

二. 使用第三方软件快速修改、切换 npm和yarn的源(以淘宝镜像为例)

npm

nrm 是一个 NPM 源管理器,允许你快速地在如下 NPM 源间切换

安装
npm install -g nrm
列出可选的源
nrm ls

* npm ---- https://registry.npmjs.org/
  cnpm --- http://r.cnpmjs.org/
  taobao - https://registry.npm.taobao.org/
  nj ----- https://registry.nodejitsu.com/
  rednpm - http://registry.mirror.cqupt.edu.cn/
  npmMirror  https://skimdb.npmjs.com/registry/
  edunpm - http://registry.enpmjs.org/

带 * 的是当前使用的源

切换

切换到taobao镜像源

nrm use taobao

   Registry has been set to: https://registry.npm.taobao.org/

测试源的响应时间

测试所有源的响应时间:

nrm test

  npm ---- 2930ms
  cnpm --- 300ms
* taobao - 292ms
  nj ----- Fetch Error
  rednpm - Fetch Error
  npmMirror  1626ms
  edunpm - Fetch Error

可以多次测量来得到更精确的结果
更多nrm使用方法访问nrm的gitHub仓库
https://github.com/Pana/nrm

yarn

yrm 是一个 yarn源管理器,允许你快速地在yarn源间切换

安装
npm install -g yrm
列出可选的源
yrm ls

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

带 * 的是当前使用的源

切换

切换到taobao镜像源

yrm use taobao

   YARN Registry has been set to: https://registry.npm.taobao.org/
测试源的响应时间

测试所有源的响应时间:

yrm test

  npm ---- 784ms
  cnpm --- 290ms
* taobao - 297ms
  nj ----- Fetch Error
  rednpm - Fetch Error
  npmMirror  1353ms
  edunpm - Fetch Error
  yarn --- Fetch Error

可以多次测量来得到更精确的结果
更多yrm使用方法访问yrm的gitHub仓库
https://github.com/i5ting/yrm

【参考文章】
https://www.jianshu.com/p/309645729b2e
http://www.16boke.com/article/detail/154
https://laravel-china.org/articles/15976/yarn-accelerate-and-modify-mirror-source-in-china

相关文章

网友评论

本文标题:修改npm和yarn源

本文链接:https://www.haomeiwen.com/subject/uofyzftx.html