首页 > 解决方案 > 如何阅读 npm“冲突对等依赖”错误消息?

问题描述

我正在尝试升级我拥有的项目的一些 npm 依赖项,并且出现“冲突的对等依赖项”错误。

我在这个网站上看到很多问题,要求帮助修复此类错误。但是,我一直在努力寻找有关这些错误实际含义的信息。我觉得如果我理解了这一点,我就有很大的机会自己想办法解决这个问题。

这是我试图解释的错误消息:

npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR! 
npm ERR! Found: @angular-devkit/build-angular@0.1102.5
npm ERR! node_modules/@angular-devkit/build-angular
npm ERR!   dev @angular-devkit/build-angular@"~0.1102.9" from the root project
npm ERR! 
npm ERR! Could not resolve dependency:
npm ERR! dev @angular-devkit/build-angular@"~0.1102.9" from the root project
npm ERR! 
npm ERR! Conflicting peer dependency: @angular/localize@11.2.10
npm ERR! node_modules/@angular/localize
npm ERR!   peerOptional @angular/localize@"^11.0.0 || ^11.2.0-next" from @angular-devkit/build-angular@0.1102.9
npm ERR!   node_modules/@angular-devkit/build-angular
npm ERR!     dev @angular-devkit/build-angular@"~0.1102.9" 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.

这可以通过在这个 Github 分支npm install的根目录中运行来重现(我使用的是 npm 。)7.10.0

我对“冲突对等依赖”错误的一般理解是,我依赖的某些包表达了对包版本规范的对等依赖,该包版本规范与我实际安装的包的版本不匹配。

例如,如果我的项目直接依赖于 packagesAB,并且我有安装的版本12.0.0A但我的版本对packageB有对等依赖,那么我将收到冲突的对等依赖错误,因为我使用的是它可能与之不兼容。^11.0.0ABA

Therefore, my best guess as to what this error message could mean is that some package I depend upon has a peer dependency on @angular/localize version spec ^11.0.0 || ^11.2.0-next, but this spec does not match the version of @angular/localize I have installed.

When I look at my package-lock.json, I do see that the node_modules/@angular-devkit/build-angular entry has an entry "@angular/localize": "^11.0.0 || ^11.2.0-next" in its peerDependencies.

However, this is the only mention of @angular/localize anywhere in this file -- or indeed in package.json. I haven't explicitly requested for it to be installed. Furthermore, it is marked as "optional": true in the peerDependenciesMeta of node_modules/@angular-devkit/build-angular. So it's surprising to see an error message related to it.

The error mentions that the specific conflicting peer dependency is @angular/localize@11.2.10. I don't see where that version number is coming from. But regardless, it actually seems to match the dependency specification underneath: if I go to semver.npmjs.com and type in @angular/localize as the package and ^11.0.0 || ^11.2.0-next as the version range, I see version 11.2.10 of the package highlighted in green, indicating that it matches the range.

So I'd really appreciate some help understanding in detail what this error message is telling me. I don't know why npm is trying to install 11.2.10 of @angular/localize, or why it thinks this conflicts with the peer dependency specification of @angular-devkit/build-angular. It feels like I might be misunderstanding this message completely.

I'm guessing this boils down to some kind of incompatibility between the latest published versions of some of the Angular packages. If anyone has any pointers on how this particular error should be fixed, that would be great -- but I'm much more interested in simply understanding what the error message is telling me, so I can work it out for myself.

Many thanks in advance!

标签: npm

解决方案


Reading through this GitHub issue, it appears my interpretation of the error message was correct, and that this is in fact a bug in npm.

This appears to have been reported as npm/cli/issues/3083; a fix has been merged, so I guess we just have to wait until it gets included in some upcoming npm release.


推荐阅读