npm&yarn更换淘宝源

做前端的开发的都知道,写 node 项目其中一个重要的点就是要配置 npm 包管理工具,但是由于 npm 官方仓库在美国,被伟大的长城防火墙屏蔽,网速相应的较慢,虽然 npm 也有国内的镜像,其中在这方面做的最好的当属淘宝镜像源。接下在就让我来为大家介绍为如何为 node.js 配置淘宝镜像源

NPM 配置

其中 node 配置淘宝镜像有三种方式

1.临时使用

1
npm --registry https://registry.npm.taobao.org install express

2.持久使用

1
npm config set registry https://registry.npm.taobao.org
  • 配置后可通过下面两种方式来验证是否成功
1
2
3
4
[[方法一:]]
npm config get registry
[[方法二:]]
npm info express

3. 配置其他模块

1
2
3
4
5
6
7
8
npm config set registry https://repo.huaweicloud.com/repository/npm/
npm config set disturl https://repo.huaweicloud.com/nodejs
npm config set sass_binary_site https://repo.huaweicloud.com/node-sass
npm config set phantomjs_cdnurl https://repo.huaweicloud.com/phantomjs
npm config set chromedriver_cdnurl https://repo.huaweicloud.com/chromedriver
npm config set operadriver_cdnurl https://repo.huaweicloud.com/operadriver
npm config set electron_mirror https://repo.huaweicloud.com/electron/
npm config set python_mirror https://repo.huaweicloud.com/python

4. 通过 cnpm 使用

1
npm install -g cnpm --registry=https://registry.npm.taobao.org

5. 清理缓存

1
npm cache clean -f

  如果采用 cnpm 使用,那么以后所有的导包命令都有 npm install XXX 变成 cnpm install XXX

Yarn 配置

1
2
3
yarn config set registry https://registry.npm.taobao.org
yarn config set disturl https://npm.taobao.org/dist
yarn config set sass-binary-site https://npm.taobao.org/mirrors/node-sass

npm 华为云配置

1
2
3
4
5
6
7
8
npm config set registry https://repo.huaweicloud.com/repository/npm/
npm config set disturl https://repo.huaweicloud.com/nodejs
npm config set sass_binary_site https://repo.huaweicloud.com/node-sass
npm config set phantomjs_cdnurl https://repo.huaweicloud.com/phantomjs
npm config set chromedriver_cdnurl https://repo.huaweicloud.com/chromedriver
npm config set operadriver_cdnurl https://repo.huaweicloud.com/operadriver
npm config set electron_mirror https://repo.huaweicloud.com/electron/
npm config set python_mirror https://repo.huaweicloud.com/python

参考文章