简体   繁体   English

YARN/NPM: 安装 package 版本兼容某些依赖版本

[英]YARN/NPM: installing package versions that are compatible to certain dependency version

Let's imagine that I have the following dependencies section in package.json file:假设我在package.json文件中有以下依赖项部分:

  "dependencies": {
    "A": "1.0.0"
  }

As well, let's assume that the current version of package A is 3.0.0 .同样,我们假设 package A 的当前版本是3.0.0 But I need version 1.0.0 of A in my project, so I explicitly specify it in package.json .但是我的项目中需要A1.0.0版本,所以我在package.json中明确指定了它。

Given that, let's assume that I need to have another package B in my project that is dependent on A .鉴于此,我们假设我的项目中需要另一个 package B依赖于A The latest version of B is also 3.0.0 , but it is incompatible with "A": "1.0.0" . B的最新版本也是3.0.0 ,但它与"A": "1.0.0"不兼容。 The correct version of B that is compatible with "A": "1.0.0" would be "B": "1.0.0" ."A": "1.0.0"兼容的B的正确版本应该是"B": "1.0.0"

The question is -问题是 -

how do I detect what version of package B is compatible with "A":"1.0.0" ?如何检测 package B的哪个版本与"A":"1.0.0"兼容?

Is there a way to automatically install the versions of packages, that are dependent on A and a compatible with "A": "1.0.0" ?有没有办法自动安装依赖于A并与"A": "1.0.0"兼容的软件包版本?

If you force the B dependencies in your project, npm install the version that you forced. 如果您在项目中强制使用B依赖性,则npm安装您强制使用的版本。

Eg 例如

"dependencies": {
    "A": "1.0.0",
    "B": "1.0.0"
}

NPM will install B 1.0.0 in your project. NPM将在您的项目中安装B 1.0.0。

To say that I tried to create a project with 说我试图用

...
"dependencies": {
   "rxjs": "5.0.1",
   "chai": "4.1.0"
},
...

Rxjs have inside the chai package dependency fixed at version 4.1.2. Rxjs的chai软件包依赖项已在版本4.1.2中修复。

I forced the 4.1.0 on my package.json and npm installed the version 4.1.0 我在我的package.json上强制安装了4.1.0版本,而npm安装了4.1.0版本

No. There's no way to install compatible package while installing node packages. 否。在安装节点程序包时无法安装兼容程序包。 You must specify them manually in package.json. 您必须在package.json中手动指定它们。


But, you may try the following: 但是,您可以尝试以下方法:

  1. Define the specific package version in package.json for only one of your main package. 在package.json中仅为您的一个主软件包定义特定的软件包版本。

  2. Install the dependency (main package). 安装依赖项(主软件包)。 npm install

  3. Don't specify the dependency module version of the main package. 不要指定主软件包的依赖模块版本。
  4. Install dependency. 安装依赖项。 npm install DEPENDENCY_PACKAGE

This might work because as far as I remember once I had defined the main package and not its dependent package and did the preceding approach and was installed compatible version. 之所以可行,是因为据我所知,一旦我定义了主软件包而不是其依赖软件包,并采用了前面的方法并安装了兼容版本。 (I had defined node engine version. But I'm not fully sure if this approach works with other packages as well.) (我已经定义了节点引擎版本。但是我不确定这种方法是否也可以与其他软件包一起使用。)

Just try and let me know if this works for you. 请尝试让我知道这是否适合您。

B should define its own dependency on a specific version of A , eg 0.0.1 . B应该定义自己对A特定版本(例如0.0.1的依赖。 In that case, when you do npm/yarn install , given you have package.json like: 在这种情况下,当您执行npm/yarn install ,给定package.json如下所示:

 "dependencies": {
    "A": "1.0.0"
    "B": "1.0.0"
  }
  1. A@1.0.0 , B@1.0.0 will be installed, 将安装A A@1.0.0B@1.0.0

since B declared it needs A@0.0.1 , 由于B声明需要A@0.0.1

  1. A@0.0.1 will be installed too, but under node_modules/B . 也将安装A@0.0.1 ,但在node_modules/B

Therefore, if the dependencies you use are well written, you should not need to manually handle these kind of problem. 因此,如果使用的依赖关系写得很好,则无需手动处理此类问题。

More information on this behavior can be found here . 有关此行为的更多信息,请参见此处

Ran into this problem when trying to install style-loader for webpack 4.尝试为 webpack 4 安装 style-loader 时遇到此问题。

how do I detect what version of package B is compatible with "A":"1.0.0"?如何检测 package B 的哪个版本与“A”:“1.0.0”兼容?

Attempting to install it using npm install style-loader tells me尝试使用npm install style-loader告诉我安装它

npm ERR: Could not resolve dependency: npm 错误:无法解析依赖项:
npm ERR. npm 错误。 peer webpack@"^5.0.0" from style-loader@3.3.1来自 style-loader@3.3.1 的 peer webpack@"^5.0.0"

Tried decreasing the version number until the install succeeded:尝试降低版本号直到安装成功:

npm install style-loader@\<3.3.0

npm ERR: Could not resolve dependency: npm 错误:无法解析依赖项:
npm ERR. npm 错误。 peer webpack@"^5.0.0" from style-loader@3.2.1来自 style-loader@3.2.1 的 peer webpack@"^5.0.0"
npm ERR! npm 错误! node_modules/style-loader node_modules/样式加载器
npm ERR. npm 错误。 style-loader@"<3.3.0" from the root project根项目中的 style-loader@"<3.3.0"

npm install style-loader@\<3.2.0 etc, until the module installed. npm install style-loader@\<3.2.0等,直到模块安装完毕。

package.json indicates that version 2.0.0 was installed. package.json表示安装了2.0.0版本。

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

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