安装角度库 - 无法解析依赖树

前段时间,我将我的通用代码从我的 Angular 应用程序中提取到库中。现在看来,我无法将针对 @angular/core@13.1 的库安装到新创建的应用程序中,该应用程序也针对 13.1。

package.json

{
  "name": "@xxx/ng-animations",
  "private": false,
  "version": "13.1.3",
  "peerDependencies": {
    "@angular/common": "^13.1.0",
    "@angular/core": "^13.1.0"
  },
  "dependencies": {
    "tslib": "^2.3.0"
  }
}

全新的应用程序 package.json

{
  ...,
  "dependencies": {
    "@angular/animations": "~13.1.0",
    "@angular/common": "~13.1.0",
    "@angular/compiler": "~13.1.0",
    "@angular/core": "~13.1.0",
    "@angular/forms": "~13.1.0",
    "@angular/platform-browser": "~13.1.0",
    "@angular/platform-browser-dynamic": "~13.1.0",
    "@angular/router": "~13.1.0",
    "rxjs": "~7.4.0",
    "tslib": "^2.3.0",
    "zone.js": "~0.11.4"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~13.1.3",
    "@angular/cli": "~13.1.3",
    "@angular/compiler-cli": "~13.1.0",
    "@types/jasmine": "~3.10.0",
    "@types/node": "^12.11.1",
    "jasmine-core": "~3.10.0",
    "karma": "~6.3.0",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage": "~2.1.0",
    "karma-jasmine": "~4.0.0",
    "karma-jasmine-html-reporter": "~1.7.0",
    "typescript": "~4.5.2"
  }
}

不知何故,这会导致以下我无法解决的版本冲突错误(我过去解决了其中一些问题)

npm i @mintplayer/ng-animations
npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR!
npm ERR! While resolving: tada@0.0.0
npm ERR! Found: @angular/core@13.1.1
npm ERR! node_modules/@angular/core
npm ERR!   @angular/core@"~13.1.0" from the root project
npm ERR!   peer @angular/core@"^13.1.0" from @mintplayer/ng-animations@13.1.3
npm ERR!   node_modules/@mintplayer/ng-animations
npm ERR!     @mintplayer/ng-animations@"*" from the root project
npm ERR!   1 more (@angular/animations)
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer @angular/core@"13.1.2" from @angular/common@13.1.2
npm ERR! node_modules/@angular/common
npm ERR!   @angular/common@"~13.1.0" from the root project
npm ERR!   peer @angular/common@"^13.1.0" from @mintplayer/ng-animations@13.1.3
npm ERR!   node_modules/@mintplayer/ng-animations
npm ERR!     @mintplayer/ng-animations@"*" 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.

我在最新版本的角度:

> ng version

Angular CLI: 13.1.3
Node: 14.18.2
Package Manager: npm 8.3.0
OS: win32 x64

Angular: 13.1.2
... animations, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1301.3
@angular-devkit/build-angular   13.1.3
@angular-devkit/core            13.1.3
@angular-devkit/schematics      13.1.3
@angular/cli                    13.1.3
@schematics/angular             13.1.3
rxjs                            7.4.0
typescript                      4.5.4

> node -v
v14.18.2

> npm -v
8.3.0

前段时间我遇到了类似的问题,但那是当我将我的应用程序从 angular 11 升级到 13 时,我能够通过检查 npm 抱怨的特定包的版本来解决它。但现在所有版本都相同(13.x.x 或 13.1.x)。

一切都针对角度 13.1,那么这个错误来自哪里?非常感谢任何帮助。

stack overflow install angular library - unable to resolve dependency tree
原文答案

答案:

作者头像

我试图安装 @angular/服务工人,但是它向我展示了一个类似的问题,我所做的是:

删除node_modules软件包
然后运行 npm update 而不是 npm install

在运行命令 npm update 之前,我的软件包 @angular/service-worker的版本已经在package.json上。

作者头像

您的全局版本是最新的,但不是项目版本。在项目中运行 npx @angular/cli@13 update @angular/core@13 @angular/cli@13 并再次安装依赖项

另一种选择是使用 --force 标志安装库