npm 同时安装同一个包的不同版本(npm@6.9 alias)

npm 无法同时安装同一个 package 的不同版本,以往遇到这种需求的时候,我的办法是自己新建一个包,然后在新建的包里安装目标包的一个版本,然后暴露出去。这样,项目就可以依赖新建的包和另一个版本,实现同时依赖两个版本的目的。

在 npm 6.9 以后,npm 可以为安装的包使用别名,可以近乎完美地解决这个问题了。elasticsearch-js 项目有一段详细说明如何使用,我把这一段翻译了一下。

同时使用多个版本

如果你同时使用多个版本的 Elasticsearch, 你需要使用对应的多个客户端。同时安装同一个包的多个版本在以前是不可能的,但是 npm 6.9 以后,你可以用别名达到目的。

安装不同版本的客户端你必须运行以下命令:

npm install <alias>@npm:@elastic/elasticsearch@<version>
   
   

比方说要同时安装 7.x6.x, 运行以下命令


   
   
  1. npm install es6@npm: @elastic/ elasticsearch@ 6
  2. npm install es7@npm: @elastic/ elasticsearch@ 7

package.json 文件就会多出下面的两行:


   
   
  1. "dependencies": {
  2. "es6": "npm:@elastic/elasticsearch@6.7.0",
  3. "es7": "npm:@elastic/elasticsearch@7.0.0"
  4. }

在代码中将刚刚定义的别名 require 进来。


   
   
  1. const { Client: Client6} = require( 'es6')
  2. const { Client: Client7} = require( 'es7')
  3. const client6 = new Client6({ node: 'http://localhost:9200'})
  4. const client7 = new Client7({ node: 'http://localhost:9201'})
  5. client6. info( console. log)
  6. client7. info( console. log)

最后,如果你想使用下一版本 Elasticsearch(即 Elasticsearch 的 master 分支) 的客户端,你可以运行下面的命令:

npm install esmaster@github:elastic/elasticsearch-js
   
   

补充

由上文的最后一条命令可看出,别名不局限于从 npm 下载的包。从 github 或者其它自定义 git 地址下载的包也可以使用别名。npm 如何安装其它来源的包可以参照官方文档

https://wpchou.github.io/2019/11/21/npm-install-multiple-versions-of-same-package.html

转载:https://blog.csdn.net/sinat_17775997/article/details/119898619?spm=1001.2014.3001.5501
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值