如何使用npm安装软件包的多个版本

如何使用npm安装软件包的多个版本

本文介绍了如何使用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问题上发布的解决方法可能适用于纯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现在支持软件包别名.它实现相同的语法如纱的用途:

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安装.例如,如果您要同时安装npm注册表版本和foobar软件包的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安装软件包的多个版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 10:03