如何使用 npm 安装多个版本的包 [英] how to install multiple versions of package using npm

查看:37
本文介绍了如何使用 npm 安装多个版本的包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于 https://github.com/npm/npm/issues/2943,npm 永远不会支持别名包和安装同一包的多个版本的能力.

Due to https://github.com/npm/npm/issues/2943, npm will never support the ability to alias packages and install multiple versions of the same package.

github issue 上发布的解决方法可能适用于纯 JS 模块,但随着 npm 成为前端包管理的标准,包现在包含各种资产,例如 CSS.

The workarounds posted on the github issue might work for pure-JS modules, but as npm becomes a standard for frontend package management, packages now include various assets such as CSS.

是否有任何解决方法可以安装同一软件包的多个版本?

Is there any workaround to install multiple versions of the same package?

我想出的最好的主意是克隆"一个包,并以稍微不同的名称发布它.

The best idea I've come up with is to "clone" a package, and publish it with a slightly different name.

例如,如果您需要多个版本的 jquery,您可以只发布名为 jquery-alias1jquery-alias2jquery-alias3 等,然后在 package.json 中设置适当的版本.

For example, if you need multiple versions of jquery, you could just publish packages called jquery-alias1, jquery-alias2, jquery-alias3 etc, and then set the appropriate versions in your package.json.

或者你可以根据版本号来命名包,例如jquery-1.11.xjquery-2.1.x等.

Or you could name the packages according to their version number, eg jquery-1.11.x, jquery-2.1.x, etc..

虽然这两种方法看起来都很草率.有没有更好的?

Both of these approaches seem sloppy though. Are there better ones?

推荐答案

截至 npm v6.9.0, npm 现在支持包别名.它实现相同的语法Yarn 用途:

As of npm v6.9.0, npm now supports package aliases. It implements the same syntax as Yarn uses:

npm install jquery2@npm:jquery@2
npm install jquery3@npm:jquery@3

这会将以下内容添加到 package.json:

This adds the following to package.json:

"dependencies": {
   "jquery2": "npm:jquery@^2.2.4",
   "jquery3": "npm:jquery@^3.4.1"
}

也可以使用此语法直接从 GitHub 安装.例如,如果您要安装包 foobar 的 npm 注册表版本和 GitHub 分支:

It is also possible to install directly from GitHub with this syntax. For example, if you want to install both the npm registry version and a GitHub fork of the package foobar:

npm install foobar
npm install foobar-fork@github:username/foobar

这篇关于如何使用 npm 安装多个版本的包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆