无法解决我的 Angular 应用程序和我的自定义 Angular 库之间的对等依赖关系

Could not resolve peer dependency between my Angular app and my custom Angular library

我已经创建了这个 Angular 库,我现在想在我的其他 Angular 应用程序中使用它,但我似乎无法处理我的对等依赖项。我曾尝试在我的应用程序 package.json 中摆弄,但似乎无法消除我的任何错误。

我尝试过的(按顺序):

错误如下:

npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR!
npm ERR! While resolving: lib-test@0.0.0
npm ERR! Found: @angular/common@13.0.3
npm ERR! node_modules/@angular/common
npm ERR!   @angular/common@"~13.0.0" from the root project
npm ERR!   peer @angular/common@"^13.0.0 || ^14.0.0-0" from @angular/cdk@13.2.2
npm ERR!   node_modules/@angular/cdk
npm ERR!     @angular/cdk@"^13.2.0" from the root project
npm ERR!     peer @angular/cdk@"^13.0.0" from @my-group/angular-lib@1.1.4
npm ERR!     node_modules/@my-group/angular-lib
npm ERR!       @my-group/angular-lib@"*" from the root project
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer @angular/common@"^13.1.0" from @my-group/angular-lib@1.1.4
npm ERR! node_modules/@my-group/angular-lib
npm ERR!   @my-group/angular-lib@"*" 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.

package.json(应用程序)

{
  "name": "lib-test",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "watch": "ng build --watch --configuration development",
    "test": "ng test"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "~13.0.0",
    "@angular/cdk": "^13.2.2",
    "@angular/common": "~13.0.0",
    "@angular/compiler": "~13.0.0",
    "@angular/core": "~13.0.0",
    "@angular/forms": "~13.0.0",
    "@angular/material": "^13.2.2",
    "@angular/platform-browser": "~13.0.0",
    "@angular/platform-browser-dynamic": "~13.0.0",
    "@angular/router": "~13.0.0",
    "@cloud-republic/angular-lib": "^1.1.4",
    "rxjs": "~7.4.0",
    "tslib": "^2.3.0",
    "zone.js": "~0.11.4"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~13.0.3",
    "@angular/cli": "~13.0.3",
    "@angular/compiler-cli": "~13.0.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.0.3",
    "karma-jasmine": "~4.0.0",
    "karma-jasmine-html-reporter": "~1.7.0",
    "typescript": "~4.4.3"
  }
}

package.json(图书馆)

{
  "name": "@my-group/angular-lib",
  "version": "1.0.6",
  "publishConfig": {
    "@my-group:registry": "https://gitlab.com/api/v4/projects/XXXXXX/packages/npm/"
  },
  "scripts": {
    "build-lib": "ng build lib-angular",
    "semantic-release": "semantic-release"
  },
  "peerDependencies": {
    "@angular/cdk": "^13.0.0",
    "@angular/common": "^13.1.0",
    "@angular/core": "^13.1.0",
    "@angular/material": "^13.2.0",
    "ngx-bootstrap": "^8.0.0",
    "rxjs": "~7.4.0"
  },
  "dependencies": {
    "tslib": "^2.3.0"
  },
  "devDependencies": {
    "@semantic-release/gitlab": "^7.0.4",
    "semantic-release": "^19.0.2"
  }
}

提前致谢

这不是对等依赖性的问题。这更多是与版本冲突有关。我猜你正在使用 npm >=7 这就是你收到此错误的原因:-

如错误日志中所述,请尝试使用以下命令解决问题:-

npm install --legacy-peer-deps

以上解决方案将解决您的问题。但是,主要问题是您的图书馆。 在您的库包中,您说使用以下包:-

 "@angular/common": "^13.1.0"

但是,在您的应用程序中,您安装了以下版本。 “@angular/common”:“~13.0.0”。 所以你的库包大于应用中安装的库包。 在您的应用程序中安装相同的版本或在您的库中降级版本。

这样,npm install命令就可以正常工作了。