简体   繁体   English

NPM 安装对等依赖的预发布版本

[英]NPM Install pre-release versions for peer dependency

We have a mono-repository using lerna.我们有一个使用 lerna 的单一存储库。 On every pull request, we would like to create a pre-release version and publish it.在每个拉取请求上,我们都希望创建一个预发布版本并发布它。

Demo Project for better understanding => react-lerna-demo演示项目以便更好地理解 => react-lerna-demo

Package structure:封装结构:

  • util-lib实用程序库
  • shared-ui --> util-lib (peer-dep) shared-ui --> util-lib (peer-dep)
  • web-app --> shared-lib & util-lib web-app --> shared-lib & util-lib

Normal releases works just fine.正常版本工作得很好。 But pre-release has the following problem.但是预发布存在以下问题。

  • When util-lib has a change, it would have version like 4.0.6-1b596d6.0当 util-lib 发生变化时,它的版本会像4.0.6-1b596d6.0
  • shared-ui has a peer dep version like ^4.0.0 shared-ui 有一个 peer dep 版本,如^4.0.0
  • Web-App is then changed to "@keth-dev/lerna-demo-util-lib": "4.0.6-1b596d6.0" (see changes然后将 Web-App 更改为"@keth-dev/lerna-demo-util-lib": "4.0.6-1b596d6.0" (参见更改

This worked before npm v7.这在 npm v7 之前有效。 But now it throws an error:但现在它抛出一个错误:

npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR! 
npm ERR! While resolving: @keth-dev/lerna-demo-web-app@4.0.5
npm ERR! Found: @keth-dev/lerna-demo-util-lib@4.0.6-1b596d6.0
npm ERR! node_modules/@keth-dev/lerna-demo-util-lib
npm ERR!   @keth-dev/lerna-demo-util-lib@"4.0.6-1b596d6.0" from the root project
npm ERR! 
npm ERR! Could not resolve dependency:
npm ERR! peer @keth-dev/lerna-demo-util-lib@"^4.0.0" from @keth-dev/lerna-demo-shared-ui@4.0.4
npm ERR! node_modules/@keth-dev/lerna-demo-shared-ui
npm ERR!   @keth-dev/lerna-demo-shared-ui@"4.0.4" from the root project
npm ERR! 
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --no-strict-peer-deps, --force, or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.

Build details => https://github.com/keth-dev/react-lerna-demo/runs/5545142338?check_suite_focus=true构建细节 => https://github.com/keth-dev/react-lerna-demo/runs/5545142338?check_suite_focus=true

Are there any solutions to support dynamic pre-release versions without legacy-peer-deps flag?是否有任何解决方案来支持没有legacy-peer-deps标志的动态预发布版本?

semver package provides an option includePrerelease to suppress the strict version match. semver包提供了一个选项includePrerelease来抑制严格的版本匹配。

If a version has a prerelease tag (for example, 1.2.3-alpha.3) then it will only be allowed to satisfy comparator sets if at least one comparator with the same [major, minor, patch] tuple also has a prerelease tag.如果一个版本有一个预发布标签(例如,1.2.3-alpha.3),那么只有当至少一个具有相同 [major, minor, patch] 元组的比较器也有一个预发布标签时,它才被允许满足比较器集.

Is there a way to pass this flag while installing using npm?有没有办法在使用 npm 安装时传递这个标志?

伺服实验

My 2 cents here... (might not be an ideal solution)我在这里的 2 美分...(可能不是理想的解决方案)

In order to support dynamic pre-releases that are referenced in peer-dependencies of another dependency without using --legacy-peer-deps , I ended up using the overrides property of package.json .为了支持在另一个依赖项的对等依赖项中引用的动态预发布,而不使用--legacy-peer-deps ,我最终使用了package.jsonoverrides属性。

Here is what the official doc says about overrides :以下是官方文档关于overrides的内容:

If you need to make specific changes to dependencies of your dependencies, for example replacing the version of a dependency with a known security issue, replacing an existing dependency with a fork, or making sure that the same version of a package is used everywhere, then you may add an override.如果您需要对依赖项的依赖项进行特定更改,例如用已知的安全问题替换依赖项的版本,用分叉替换现有的依赖项,或者确保在任何地方都使用相同版本的包,那么你可以添加一个覆盖。

Overrides provide a way to replace a package in your dependency tree with another version, or another package entirely.覆盖提供了一种将依赖关系树中的包替换为另一个版本或完全替换为另一个包的方法。 These changes can be scoped as specific or as vague as desired.这些更改的范围可以根据需要具体或模糊。

For instance,例如,

{
    "dependencies": {
        "foo": "1.O.0", // has a peer-dependency to bar@^1.0.0
        "bar": "1.1.0-pre.1" // would normaly fail as bar@1.1.0-pre.1 is not part of bar@^1.0.0 according to NPM implementation of semver
    },
    "overrides": {
        "bar": "$bar" // the override is defined as a reference to the dependency
    }
}

Again, that might not be the ideal solution but it works.同样,这可能不是理想的解决方案,但它确实有效。

However keep in mind that having the override disables the peer dependency validity checks for the bar package completely.但是请记住,覆盖会完全禁用对bar包的对等依赖项有效性检查。

I plan on adapting my tooling in order to prevent overrides during a release build for instance.例如,我计划调整我的工具以防止在发布构建期间overrides

Any comment on why I should not do this is welcomed.欢迎任何关于我为什么不应该这样做的评论。

Note that there has been discussions last year about this exact issue in the NPM RFC but nothing came out of it: https://github.com/npm/rfcs/pull/397请注意,去年已经在 NPM RFC 中讨论过这个确切的问题,但没有任何结果: https ://github.com/npm/rfcs/pull/397

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

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