Skip to main content

NPM 安装多个版本

· One min read

使用以下命令可以安装同一个包的不同版本:

npm install <alias>@npm:<package_name>@<version>

比如:

npm install es6@npm:@elastic/elasticsearch@6
npm install es7@npm:@elastic/elasticsearch@

然后使用以下代码导入使用:

const { Client: Client6 } = require('es6')
const { Client: Client7 } = require('es7')

const client6 = new Client6({
cloud: { id: '<cloud-id>' },
auth: { apiKey: 'base64EncodedKey' }
})
const client7 = new Client7({
cloud: { id: '<cloud-id>' },
auth: { apiKey: 'base64EncodedKey' }
})

client6.info().then(console.log, console.log)
client7.info().then(console.log, console.log)