myesn

myEsn2E9

hi
github

npm/Yarn: 替换国内仓库镜像源地址

简介#

npm 和 yarn 更改默认下载仓库中心地址,一定要设置不然慢死人妈的

一般都是用淘宝团队提供的 https://npmmirror.com 镜像站。

自动化换源设置#

推荐使用开源工具 chsrc 自动设置。

操作步骤:

  1. Releases 页面下载对应操作系统的可执行文件,我这里使用的是 Windows,所以下载 chsrc-x64-windows.exe
  2. 然后打开终端,执行 chsrc-x64-windows.exe list 查看支持换源的软件等
  3. 由于我们这是 nodejs,所以在终端执行 chsrc-x64-windows.exe set nodejs,然后等待执行完毕即可,这过程是自动的。

手动全局设置#

npm 全局设置#

npm config set registry https://registry.npmmirror.com
npm config set disturl https://npmmirror.com/mirrors/node

# 下面的根据自己的情况设置
npm config set electron_mirror https://registry.npmmirror.com/electron/
npm config set sass_binary_site https://registry.npmmirror.com/node-sass/

yarn 全局设置#

yarn config set registry https://registry.npmmirror.com -g
yarn config set disturl https://npmmirror.com/mirrors/node -g

# 下面的根据自己的情况设置
yarn config set electron_mirror https://registry.npmmirror.com/electron/ -g
yarn config set sass_binary_site https://registry.npmmirror.com/node-sass/ -g

yarn 手动为单个项目设置#

在项目根目录创建 .yarnrc 文件,再将以下内容粘贴进去保存:

registry https://registry.npmmirror.com
disturl https://npmmirror.com/mirrors/node

# 下面的根据自己的情况设置
electron_mirror https://registry.npmmirror.com/electron/
sass_binary_site https://registry.npmmirror.com/node-sass/

注意#

以上配置只会在添加新依赖的时候被使用,旧依赖的包下载地址都被保存在了 lock 文件中,需要手动修改,比如 yarn.lock 的修改方式如下:
yarn.lock 文件中的 https://registry.yarnpkg.com 全部替换为 https://registry.npmmirror.com

参考#

載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。