You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

package.json中overrides配置失效及EOVERRIDE错误的技术咨询

为何package.json中的overrides配置不生效?

问题重现

不生效的配置(含overrides)

{
    "dependencies": {
        "@angular/animations": "^15.0.0",
        "@angular/cdk": "15.1.2",
        "@angular/common": "15.1.2",
        ...
    },
    "overrides": {
        "@angular/animations": "15.1.2"
    }
}

运行npm install或npm install --force时会抛出以下错误:

npm ERR! code EOVERRIDE
npm ERR! Override for @angular/animations@^15.0.0 conflicts with direct dependency

生效的配置(直接修改依赖版本)

{
    "dependencies": {
        "@angular/animations": "15.1.2",
        "@angular/cdk": "15.1.2",
        "@angular/common": "15.1.2",
        ...
    }
}

我的疑问

  1. 我的overrides配置是不是要将@angular/animations强制设置为15.1.2?
  2. 参考npm官方文档的说明,我对以下三种情况存在困惑:

除非依赖项与override的版本规范完全一致,否则不得为直接依赖的包设置override。为简化操作,override可通过$前缀引用直接依赖的版本规范。

文档示例代码:

{
  "dependencies": {
    "foo": "^1.0.0"
  },
  "overrides": {
    // BAD, will throw an EOVERRIDE error
    // "foo": "^2.0.0"
    // GOOD, specs match so override is allowed
    // "foo": "^1.0.0"
    // BEST, the override is defined as a reference to the dependency
    "foo": "$foo",
    // the referenced package does not need to match the overridden one
    "bar": "$foo"
  }
}

具体疑问:

  • BAD:是不是意味着override版本不能比依赖项新?
  • GOOD:是不是override版本与依赖项一致就允许,这正是我正在做的?
  • BEST:无法理解该配置的含义。

问题解答

为什么你的overrides配置报错?

npm的overrides规则明确:直接依赖的包,override的版本规范必须和dependencies里的完全一致。你的dependencies里@angular/animations是^15.0.0,但overrides里写的是15.1.2——这两个版本规范并不完全匹配,所以触发了EOVERRIDE错误。你想通过overrides把@angular/animations固定到15.1.2的意图是对的,但不符合npm的规则要求。

对文档三种情况的解释

  • BAD情况:不是单纯不能比依赖项新,而是override的版本规范和dependencies里的完全不一致。比如dependencies是^1.0.0,override写^2.0.0,两者版本范围完全不重叠,属于冲突所以报错;就算写~1.0.0(和^1.0.0范围接近但不完全一致),同样会报错。
  • GOOD情况:是的,但这里的“一致”指版本规范字符串完全匹配。比如你dependencies里是^15.0.0,override也得写^15.0.0,而不是15.1.2——这就是你出错的核心:你混淆了“具体版本”和“版本范围规范”的差异。
  • BEST情况:"foo": "$foo"是让override直接引用dependencies里foo的版本规范。比如dependencies里foo是^1.0.0,这个override就等价于"foo": "^1.0.0"。好处是后续修改dependencies里foo的版本时,overrides会自动同步,不用手动改两次。另外"bar": "$foo"是把bar的版本强制设置成和foo一样的规范,适合需要让两个包版本保持一致的场景。

正确的做法

如果你想把@angular/animations固定到15.1.2,直接修改dependencies里的版本号是最直接的方式(即你第二种生效的配置)。如果一定要用overrides(比如同时强制子依赖也用这个版本),需要先把dependencies里的版本改成15.1.2,然后overrides里写15.1.2或者"@angular/animations": "$@angular/animations"。


内容的提问来源于stack exchange,提问作者Thomas David Kehoe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 15:20:22