ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

npm 配置 registry 以及使用 nrm

2019-12-06 14:03:22  阅读:901  来源: 互联网

标签:npm nrm cnpm registry https org


npm 配置 registry 以及使用 nrm

 

由于众所周知的原因,我们的内网链接互联网时非常不稳定,速度慢而且经常下载失败。为了提高下载安装 npm 包的体验,很多人都会把 npm 的 registry 配置成国内镜像,我们一般用的比较多的就是淘宝镜像。

镜像是一种存储的形式。一个磁盘上的东西,在另一个磁盘拷贝一份一摸一样的副本,这就是镜像。

淘宝镜像是 npm 官方服务器的同步版本,所以我们从淘宝镜像获取的 npm 包等同于从官方获取 npm 包。这样做的好处是淘宝镜像也在内网,所以稳定且速度快。淘宝镜像地址: https://npm.taobao.org/

 

配置 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 命令下载包,将会从淘宝镜像开始下载,稳定且速度快。

 

CNPM

cnpm 是一个维护私有 npm 的解决方案,淘宝就是使用了 cnpm 搭建了一个属于内网的 npm 镜像。

可以安装 cnpm 包来下载安装淘宝镜像上的 npm 包,而不必修改 npm 的 register。

cnpm 的命令基本和 npm 差不多,所有使用起来很方便。

安装 cnpm

建议全局安装

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

 

使用 cnpm

cnpm 命令和 npm 差不多,就像 npm 一样使用。

$ cnpm install webpack 

或者

$ npx cnpm install webpack

 

 

使用 nrm 管理 registry (推荐)

nrm 是 NPM registry manager(NPM注册管理器)的缩写,可以更简单、快捷的管理 registry。

nrm 本身并不是装包工具,nrm 是维护了几个常用的 npm 的 registry 地址,方便我们简单、快捷地切换。

 

安装 nrm

nrm 建议全局安装

$ npm install nrm -g

 

展示 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://registry.nodejitsu.com/
 npmMirror -- https://skimdb.npmjs.com/registry/
 edunpm ----- http://registry.enpmjs.org/

 

如果输入 npm config ls 命令查看 metrics-registry 就会得到相同的信息。

$ npm config ls
> metrics-registry = "https://registry.npm.taobao.org/"

 

改变 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://registry.nodejitsu.com/
 npmMirror -- https://skimdb.npmjs.com/registry/
 edunpm ----- http://registry.enpmjs.org/

同样,如果想使用 nrmnpm 的registry 改为淘宝镜像,如下:

$ npx nrm use taobao  
> Registry has been set to: https://registry.npm.taobao.org/

 

参考: https://github.com/Pana/nrm

标签:npm,nrm,cnpm,registry,https,org
来源: https://www.cnblogs.com/luciolu/p/11994808.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有