一、说明
在前端开发的时候使用国外的镜像源速度很慢并且容易下载失败,有时候需要尝试多次才有可能下载成功,很麻烦,因此可以切换为国内镜像源,下面为常用的npm,yarn,pnpm切换国内镜像源(以淘宝为例)的方式。

二、NPM切换镜像源
1. 查看当前的镜像源。
“`
npm config get registry
“`

2. 设置为淘宝源
“`
npm config set registry https://registry.npm.taobao.org
“`

3. 还原默认源
“`
npm config set registry https://registry.npmjs.org/
“`

4. 临时使用
上面那种设置是全局的,以后每次都会自动读取已经设置好的源,如果只是一次性使用,可以使用下面的命令
“`
npm –registry https://registry.npm.taobao.org install XXX(模块名)
“`

4.使用cnpm
cnpm是一个命令,用它来代替npm
“`
npm install -g cnpm –registry=https://registry.npm.taobao.org
cnpm install XXX(模块名)
“`

5.使用nrm
“`
npm install -g nrm
nrm use taobao
nrm ls # 查看当前可用源命令
“`

### 三、Yarn 切换镜像源
1. 查看当前镜像源
“`
yarn config get registry
“`
2. 设置为淘宝镜像源
“`
yarn config set registry https://registry.npmmirror.com
“`
3. 切回原镜像源
“`
yarn config set registry https://registry.yarnpkg.com
“`
### 三、pnpm 切换镜像源

1. 查看当前镜像源
“`
pnpm config get registry
“`

2. 设置为淘宝镜像源
“`
pnpm config set registry https://registry.npmmirror.com
“`

3. 切回原镜像源
“`
pnpm config set registry https://registry.npmjs.org
“`