• npm 配置 registry 以及使用 nrm


    由于众所周知的原因,我们的内网链接互联网时非常不稳定,速度慢而且经常下载失败。为了提高下载安装 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://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/"

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

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

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

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

  • 相关阅读:
    超详细教程2021新版oracle官网下载Windows JAVA-jdk11并安装配置(其他版本流程相同)
    个人总结
    6.15 团队项目心得
    五月团队项目收获
    八大排序算法读书笔记
    设计模式读书笔记3
    设计模式读书笔记2
    结对编程收获
    设计模式读书笔记
    UI-12组结对编程作业总结
  • 原文地址:https://www.cnblogs.com/luciolu/p/11994808.html
Copyright © 2020-2023  润新知