1?? 前言
無入侵,指的是不通過 npm config set registry http://registry.npm.taobao.org/ 等類型的命令修改 registry 達到加速的目的。
如果你寫過包上傳到 npm ,這種替換 registry 的方式在你使用 npm publish 進行發(fā)包的時候需要切換回 npm 官方的鏡像地址。
目前我用的就是替換 registry 這種方式解決的。但是像 node-sass 這種二進制文件下載還是用的國外地址。所以我有下面一堆的 registry 需要替換
npm config set sass_binary_site https://npm.taobao.org/mirrors/node-sass # node-sass 二進制包鏡像
npm config set electron_mirror https://npm.taobao.org/mirrors/electron/ # electron 二進制包鏡像
npm config set puppeteer_download_host https://npm.taobao.org/mirrors # puppeteer 二進制包鏡像
npm config set chromedriver_cdnurl https://npm.taobao.org/mirrors/chromedriver # chromedriver 二進制包鏡像
npm config set operadriver_cdnurl https://npm.taobao.org/mirrors/operadriver # operadriver 二進制包鏡像
npm config set phantomjs_cdnurl https://npm.taobao.org/mirrors/phantomjs # phantomjs 二進制包鏡像
npm config set selenium_cdnurl https://npm.taobao.org/mirrors/selenium # selenium 二進制包鏡像
npm config set node_inspector_cdnurl https://npm.taobao.org/mirrors/node-inspector # node-inspector 二進制包鏡像
npm config set node_sqlite3_binary_host_mirror https://npm.taobao.org/mirrors
npm config set sass_binary_site https://npm.taobao.org/mirrors/node-sass/
npm config set phantomjs_cdnurl https://npm.taobao.org/mirrors/phantomjs/
npm config set electron_mirror https://npm.taobao.org/mirrors/electron/
yarn config set sass_binary_site https://npm.taobao.org/mirrors/node-sass # node-sass 二進制包鏡像
yarn config set electron_mirror https://npm.taobao.org/mirrors/electron/ # electron 二進制包鏡像
yarn config set puppeteer_download_host https://npm.taobao.org/mirrors # puppeteer 二進制包鏡像
yarn config set chromedriver_cdnurl https://npm.taobao.org/mirrors/chromedriver # chromedriver 二進制包鏡像
yarn config set operadriver_cdnurl https://npm.taobao.org/mirrors/operadriver # operadriver 二進制包鏡像
yarn config set phantomjs_cdnurl https://npm.taobao.org/mirrors/phantomjs # phantomjs 二進制包鏡像
yarn config set selenium_cdnurl https://npm.taobao.org/mirrors/selenium # selenium 二進制包鏡像
yarn config set node_inspector_cdnurl https://npm.taobao.org/mirrors/node-inspector # node-inspector 二進制包鏡像
非常的不方便,最近發(fā)現(xiàn)了一個無入侵的解決工具,試用了一會,非常好用。
2?? tbify
Github:tbify
原理
基于環(huán)境變量對國內(nèi)請求速度欠佳的資源地址進行了替換,具體分為兩點:
- 通過環(huán)境變量令包管理工具(
npm、npx、yarn、pnpm、pnpx)使用淘寶源安裝依賴; - 通過環(huán)境變量令諸如
nvm、node-sass、Electron、Puppeteer、Cypress、Sharp等包使用淘寶鏡像安裝其自身所需資源。
以上,本工具對包管理工具本身零侵入,同時,對環(huán)境變量的設置也是一次性的,并不會產(chǎn)生任何的副作用,請放心使用。
安裝
# npm
npm install tbify --global
# yarn
yarn global add tbify
# pnpm
pnpm add --global tbify
使用
對于常用的包管理命令,tbify 提供了使用淘寶 NPM 鏡像的等價命令,除了發(fā)布包到 npm 時必須使用 npm publish 外,都可以使用等價命令進行相關操作:
| 原命令 | 使用淘寶 NPM 鏡像的命令 | 示例 |
|---|---|---|
nvm |
tnvm (或 tbify nvm) |
tnvm install 8.0.0 |
npm |
tnpm (或 tbify npm) |
tnpm install react |
npx |
tnpx (或 tbify npx) |
tnpx kill-port 3000 |
yarn |
tyn (或 tbify yarn) |
tyn add react |
pnpm |
tpm (或 tbify pnpm) |
tpm add react |
pnpx |
tpx (或 tbify pnpx) |
tpx kill-port 3000 |
對于其他命令,在使用時加上 tbify 前綴即可,比如:
tbify printenv npm_config_registry
# -> https://r.npm.taobao.org
3?? 注意事項
- 如果之前有替換二進制文件下載地址最好改回來
-
npm或yarn的registry改回官方的
關于我
SunSeekerX,前端開發(fā)、Nodejs開發(fā)、小程序、uni-app開發(fā)、等等
喜歡探討技術實現(xiàn)方案和細節(jié),完美主義者,見不得bug。
Github:https://github.com/SunSeekerX
個人博客:https://yoouu.cn/
個人在線筆記:https://sunseekerx.yoouu.cn/