npm 是一個包管理器。Node.js 自帶。
cnpm 是 npm 的阿里版,用的阿里源。
yarn 是另一個包管理器,不自帶,需要另外裝。可以單獨裝,也可以用 npm 裝。
tyarn 是 yarn 的阿里版,用的阿里源。
安裝 cnpm
$ npm install -g cnpm --registry=https://registry.npm.taobao.org
npm 怎么用,cnpm就怎么用。
安裝 tyarn
npm i yarn tyarn -g
yarn 怎么用,tyarn就怎么用。
npm 換淘寶源。
npm config set registry https://registry.npm.taobao.org
換回來。
npm config set registry https://registry.npmjs.org
看一下現在是什么源。
npm config get registry
yarn 同理。
yarn config set registry https://registry.npmjs.org
yarn config set registry https://registry.npm.taobao.org
yarn config get registry
npm 設置代理
npm config set proxy=http://127.0.0.1:8087
npm config set https-proxy=http://127.0.0.1:8087
npm 取消代理
npm config delete proxy
npm config delete https-proxy
yarn 設置代理
yarn config set proxy http://XXX
yarn config set https-proxy http://XXX
yarn 取消代理
yarn config delete proxy
yarn config delete https-proxy
還有一個指定目錄換源的方法。
新建一個.npmrc 到需要 yarn 的目錄
registry=https://registry.npm.taobao.org
內容如上。
對於個別很難下載的庫,還可以通過設置 SASS_BINARY_SITE 解決。
參考:https://segmentfault.com/a/1190000005921721
綜上所述,裝依賴遇到困難時,至少有4種方法。
- 使用 cnpm / tyarn
- 更換淘寶源 / .npmrc 設置源
- 設置代理
- 設置
SASS_BINARY_SITE
以上。