简体   繁体   English

如何管理同一个 NPM 依赖的多个版本?

[英]How to manage multiple versions of the same NPM dependency?

Situation情况

I've written a bunch of D3.js charts using the latest version of D3 (4.9.1) .我使用最新版本的D3 (4.9.1)编写了一堆D3.js图表。

However I also need to include the occasional C3.js chart in my app, problem is- C3 requires D3 v3.5.0 .但是,我还需要在我的应用程序中包含偶尔的C3.js图表,问题是 - C3 需要D3 v3.5.0


What I've considered so far到目前为止我所考虑的

  • Forking C3 to update it to the latest version of D3 (it's not really feasible though)分叉 C3 以将其更新到 D3 的最新版本(虽然这并不可行)

  • Using a different package manager, such as Yarn使用不同的包管理器,例如 Yarn

  • Just forgetting about C3.... (don't want to do this, as it will involve a lot of re-work!)只是忘记了 C3 ......(不想这样做,因为它会涉及很多返工!)

  • Specifying a URL of an older version in the bower.json.在 bower.json 中指定旧版本的 URL。 However, I still was not able to reference to just that version for C3, and the latest for everything else.但是,我仍然无法仅引用 C3 的那个版本,以及其他所有内容的最新版本。

     "d3": "^4.9.1", "d3-3.5.0": "https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.0/d3.min.js"

Question问题

Is it possible to manage multiple versions of the same dependency, cleanly?是否可以干净地管理同一依赖项的多个版本? And if not, what would be a sensible work-around?如果没有,什么是明智的解决方法?

TLDR TLDR

Aliased versions of a dependency can be created with both NPM or Yarn using:可以使用 NPM 或 Yarn 创建依赖项的别名版本:

 npm install <package_name_alias>@npm:<package_name>

Intro介绍

Having multiple versions of the same dependency is really not ideal if it can be helped.如果可以提供帮助,那么拥有相同依赖项的多个版本确实并不理想。 But if, for example you're migrating to the latest version of a given package, while continuing to support legacy features in the interim, then it may be necessary.但是,例如,如果您正在迁移到给定软件包的最新版本,同时在过渡期间继续支持旧版功能,那么它可能是必要的。

Ensure you're using a recent version of NPM (at least v6.9.0 when support for this was added).确保您使用的是最新版本的 NPM(添加了对此的支持时至少为v6.9.0 )。

Install安装

So, to for example install both Vue 2, and the latest Vue 3 with NPN package aliases , we would do:因此,例如,要同时安装 Vue 2 和带有 NPN 包别名的最新 Vue 3,我们会这样做:

npm i vue
npm i vue-legacy@npm:vue@2.6.14

Or, with Yarn:或者,使用纱线:

yarn add vue
yarn add vue-legacy@npm:vue@2.6.14

Import进口

Then, when it comes time to use the dependency,然后,当需要使用依赖项时,

import Vue from 'vue'; // Will use the latest version
import Vue from 'vue-legacy'; // Will use V 2.6.14

Package.json包.json

In the package.json, this will look like:在 package.json 中,这将如下所示:

"dependencies": {
  "vue": "^3.2.33",
  "vue-legacy": "npm:vue@^2.6.14"
}

You can also add this into your package.json manually, remove the lock file, and run npm install / yarn to fetch您也可以手动将其添加到您的 package.json 中,删除锁定文件,然后运行npm install / yarn来获取

Alternate Sources备用来源

You can also install packages directly from GitHub using this method.您也可以使用此方法直接从 GitHub 安装包。 Useful to get a specific version, even if not yet published to NPM, or if you wish to use your own fork of the project.即使尚未发布到 NPM,或者如果您希望使用您自己的项目分支,也可以用于获取特定版本。

npm install package-name@github:username/repository

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM