痛点
作为前端开发者我们经常会使用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的源(以淘宝镜像为例)
(1)分别修改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 taobaoRegistry has been set to: https://registry.npm.taobao.org/
测试源的响应时间
测试所有源的响应时间:
nrm testnpm ---- 2930mscnpm --- 300ms* taobao - 292msnj ----- Fetch Errorrednpm - Fetch ErrornpmMirror 1626msedunpm - Fetch Error
可以多次测量来得到更精确的结果
更多nrm使用方法访问nrm的gitHub仓库
https://github.com/Pana/nrm
yarn
yrm 是一个 yarn源管理器,允许你快速地在yarn源间切换
安装
npm install -g yrm
列出可选的源
yrm lsnpm ---- 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 taobaoYARN Registry has been set to: https://registry.npm.taobao.org/
测试源的响应时间
测试所有源的响应时间:
yrm testnpm ---- 784mscnpm --- 290ms* taobao - 297msnj ----- Fetch Errorrednpm - Fetch ErrornpmMirror 1353msedunpm - Fetch Erroryarn --- Fetch Error
可以多次测量来得到更精确的结果
更多yrm使用方法访问yrm的gitHub仓库
https://github.com/i5ting/yrm
(2)使用双源管理工具cgr (特感谢缤纷扫落叶推荐)
安装
npm install -g cgr
使用方法和前两种差别不大,下边附上使用方法网址
https://www.npmjs.com/package/cgr
