由于众所周知的原因,我们的内网链接互联网时非常不稳定,速度慢而且经常下载失败。为了提高下载安装 npm
npm
的 registry 配置成国内镜像,我们一般用的比较多的就是淘宝镜像。
镜像是一种存储的形式。一个磁盘上的东西,在另一个磁盘拷贝一份一摸一样的副本,这就是镜像。
淘宝镜像是 npm
npm
npm
https://npm.taobao.org/
1. 配置 registry 为淘宝镜像
首先查看配置信息:
$ npm config ls
注意输出信息中,有一条 metrics-registry
,默认是指向 npm
官方地址:
> metrics-registry = "https://registry.npmjs.org/"
我们可以使用命令把他改成淘宝镜像地址
$ npm config set registry https://registry.npm.taobao.org/
修改完成再输入命令查看配置文件,会发现 metrics-registry
已经发生了修改。
$ npm config ls
> metrics-registry = "https://registry.npm.taobao.org/"
这样,我们就修改完成了,之后使用 npm
命令下载包,将会从淘宝镜像开始下载,稳定且速度快。
2. CNPM
cnpm
npm
cnpm
npm
镜像。
可以安装 cnpm
cnpm
npm
npm
的 register。
cnpm
npm
差不多,所有使用起来很方便。
2.1 安装 cnpm
建议全局安装
$ npm install cnpm -g --registry=https://r.npm.taobao.org
2.2 使用 cnpm
cnpm
npm
npm
一样使用。
$ cnpm install webpack
或者
$ npx cnpm install webpack
3. 使用 nrm
管理 registry (推荐)
nrm
是 NPM registry manager(NPM注册管理器)的缩写,可以更简单、快捷的管理 registry。
nrm
nrm
npm
的 registry 地址,方便我们简单、快捷地切换。
3.1 安装 nrm
nrm
建议全局安装
$ npm install nrm -g
3.2 展示 nrm
维护的所有 registry
安装完成后,输入查看命令
$ nrm ls
如果直接运行 nrm
命令提示报错的话,可以尝试下面的命令,如果仍旧无法解决,可以查看报错信息来找到解决方案
$ npx nrm ls
查看数据结果,可以看到 nrm
帮我们维护了 npm
yarn
cnpm
taobao
nj
npmMirror
edunpm
这个7个环境,前面的 *
(星号) 表示当前正在使用的环境。
>
npm -------- https://registry.npmjs.org/
yarn ------- https://registry.yarnpkg.com/
cnpm ------- http://r.cnpmjs.org/
* taobao ----- https://registry.npm.taobao.org/
nj --------- https:///
npmMirror -- https://skimdb.npmjs.com/registry/
edunpm ----- http:///
如果输入 npm config ls
metrics-registry
就会得到相同的结果。
$ npm config ls
> metrics-registry = "https://registry.npm.taobao.org/"
3.3 改变 npm
的 registry
使用 use
命令,如下:
$ npx nrm use [name]
[name] 必须得是,刚才 nrm
维护的 registry ,也就是 nrm ls
命令展示的那7个 registry 其中之一。
例如将 npm
的 registry 再改回官方地址:
$ npx nrm use npm
> Registry has been set to: https://registry.npmjs.org/
可以看到输入命令之后,会提示 “Registry 已经被注册为 ***” 的提示语。
然后查看 npm
的配置信息:
$ npm config ls
并且查看 nrm
的 registry 列表,发现星号也移动到了 npm
这一行:
$ npx nrm ls
>
* npm -------- https://registry.npmjs.org/
yarn ------- https://registry.yarnpkg.com/
cnpm ------- http://r.cnpmjs.org/
taobao ----- https://registry.npm.taobao.org/
nj --------- https:///
npmMirror -- https://skimdb.npmjs.com/registry/
edunpm ----- http:///
同样,如果想使用 nrm
把 registry 改为淘宝镜像,如下:
$ npx nrm use taobao
> Registry has been set to: https://registry.npm.taobao.org/