简体   繁体   English

NPM peerDependencies 如何指定主版本范围

[英]NPM peerDependencies how to specify major version range

Case案子

I'm developing a common react component for 2 our projects which uses 2 different version of react-bootstrap.我正在为我们的 2 个项目开发一个通用的反应组件,它使用 2 个不同版本的 react-bootstrap。

First one: "react-bootstrap": "^0.33.1"
Second one: "react-bootstrap": "^1.0.0-beta.16"

In library's packages json I have:在图书馆的包 json 我有:

{
  "peerDependencies": {
    "react": "^16.13.0",
    "react-bootstrap": "^0.33.1",
    "react-dom": "^16.13.0"
  }
}

The component works fine with both (used API remains the same between these 2 versions).该组件适用于两者(使用的 API 在这两个版本之间保持相同)。

The problem问题

When I'm trying to install that dependency in second project I get next error:当我尝试在第二个项目中安装该依赖项时,出现下一个错误:

npm ERR! Could not resolve dependency:
npm ERR! peer react-bootstrap@"^0.33.1" from @company/lib@1.1.6
npm ERR! node_modules/@company/lib
npm ERR!   @company/lib@"1.1.6" from the root project
npm ERR! 
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force, or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.

The question is问题是

How can I specify in peerDependencies that lib can be used in both, 0.33.1 and 1.0.0 versions?如何在peerDependencies中指定 lib 可以在 0.33.1 和 1.0.0 版本中使用? Without forcing npm i to ignore these errors using --force flag.在不强制npm i使用--force标志忽略这些错误的情况下。

So, it supports OR operator "react-bootstrap": "^0.33.1 || ^1.0.0-beta.16"因此,它支持 OR 运算符"react-bootstrap": "^0.33.1 || ^1.0.0-beta.16"

but it doesn't solve the problem 'cause the package is building using old version.但这并不能解决问题,因为 package 正在使用旧版本构建。

So the solution was in just:所以解决方案只是:

  1. Write 2 scripts that patch package.json with correct version in peerDeeps and change package name (from @company/package to @company/package-legacy )在 peerDeeps 中编写 2 个用正确版本修补 package.json 的脚本,并更改 package 名称(从@company/package@company/package-legacy
  2. Update CI/CD pipeline to build both versions of package更新 CI/CD 管道以构建 package 的两个版本

Not so clean, but works perfect.不是很干净,但效果很好。

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

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