一:npm 配置 registry
1:首先查看配置信息:npm config ls
npm config ls
2:输出信息中,有一条 metrics-registry ,默认是指向 npm 官方地址
>metrics-registry = "https://registry.npmjs.org/"
3:输出信息中,有一条 metrics-registry ,默认是指向 npm 官方地址
npm config set registry https://registry.npm.taobao.org/
4:修改完成再输入命令查看配置文件,会发现 metrics-registry 已经发生了修改。
$ npm config ls\
> metrics-registry = "https://registry.npm.taobao.org/"
5:临时与持久配置
临时使用
npm --registry [registry.npm.taobao.org](https://link.juejin.cn?target=https%3A%2F%2Fregistry.npm.taobao.org "https://registry.npm.taobao.org") install express
持久使用
npm config set registry [registry.npm.taobao.org](https://link.juejin.cn?
target=https%3A%2F%2Fregistry.npm.taobao.org "https://registry.npm.taobao.org")
配置后可通过下面方式来验证是否成功
npm config get registry 或 npm info express
二. CNPM 配置安装
1:建议全局安装
$ npm install cnpm -g --registry=https://r.npm.taobao.org
2: 使用 cnpm
cnpm install webpack
三:使用 nrm 管理或者查看 registry (推荐)
1:nrm 建议全局安装
$ npm install nrm -g
2 :展示 nrm 维护的所有 registry
安装完成后,输入查看命令
$ nrm ls
3:如果直接运行 nrm 命令提示报错的话,可以尝试下面的命令,如果仍旧无法解决,可以查看报错信息来找到解决方案
$ npx nrm ls
4:查看数据结果,可以看到 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://registry.nodejitsu.com/
npmMirror -- https://skimdb.npmjs.com/registry/
edunpm ----- http://registry.enpmjs.org/
5:如果输入 npm config ls 命令查看 metrics-registry 就会得到相同的结果。
$ npm config ls
> metrics-registry = "https://registry.npm.taobao.org/"
四:改变或者切换 npm 的 registry
1:使用 use 命令,如下:
$ npx nrm use [name]
2: [name] 必须得是,刚才 nrm 维护的 registry ,也就是 nrm ls 命令展示的那7个 registry 其中之一。
例如将 npm 的 registry 再改回官方地址:
$ npx nrm use npm
> Registry has been set to: https://registry.npmjs.org/
3:可以看到输入命令之后,会提示 “Registry 已经被注册为***” 的提示语。
然后查看 npm 的配置信息:
$ npm config ls
4:并且查看 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://registry.nodejitsu.com/
npmMirror -- https://skimdb.npmjs.com/registry/
edunpm ----- http://registry.enpmjs.org/
5: 同样,如果想使用 nrm 把 ``registry 改为淘宝镜像,如下:
$ npx nrm use taobao
> Registry has been set to: https://registry.npm.taobao.org/
五:卸载
npm uninstall -g cnpm --registry=registry.npm.taobao.org
注册 npm set registry registry.npm.taobao.org
解析 npm set disturl npm.taobao.org/dist
安装 npm install -g cnpm --registry=registry.npm.taobao.org