npm 解析时出错...无法解析Angular Library中的依赖项

ajsxfq5m  于 2023-06-06  发布在  Angular
关注(0)|答案(1)|浏览(216)

所以我正在写我的第一个组件库。到目前为止,一切都很顺利,但我现在正在测试我的库安装到新的项目。
我的库项目的package.json文件有以下设置(为了提高可读性,我在这里减少/删除了很多代码)

{
  "name": "@myfirstlibrary/angular-helpers",
  "private": false,
  "peerDependencies": {
    "@angular/animations": "^15.2.9",
    "@angular/common": "^15.2.9",
    "@angular/compiler": "^15.2.9",
    "@angular/core": "^15.2.9",
    "@angular/forms": "^15.2.9",
    "@angular/platform-browser": "^15.2.9",
    "@angular/platform-browser-dynamic": "^15.2.9",
    "@angular/router": "^15.2.9",
    "rxjs": "7.8.1",
    "zone.js": "~0.13.0"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "^15.2.8",
    "@angular-eslint/builder": "^15.2.1",
    "@angular-eslint/eslint-plugin": "^15.2.1",
    "@angular-eslint/eslint-plugin-template": "^15.2.1",
    "@angular-eslint/schematics": "^15.2.1",
    "@angular-eslint/template-parser": "^15.2.1",
    "@angular/animations": "^15.2.9",
    "@angular/cli": "^15.2.8",
    "@angular/common": "^15.2.9",
    "@angular/compiler": "^15.2.9",
    "@angular/compiler-cli": "^15.2.9",
    "@angular/core": "^15.2.9",
    "@angular/forms": "^15.2.9",
    "@angular/language-service": "^15.2.9",
    "@angular/platform-browser": "^15.2.9",
    "@angular/platform-browser-dynamic": "^15.2.9",
    "@angular/router": "^15.2.9"
    }
}

现在我使用Angular CLI创建一个新项目,它使用Angular 16...类似这样的东西(我再次减少了package.json中的内容)

"dependencies": {
    "@angular/animations": "^16.0.0",
    "@angular/common": "^16.0.0",
    "@angular/compiler": "^16.0.0",
    "@angular/core": "^16.0.0",
    "@angular/forms": "^16.0.0",
    "@angular/platform-browser": "^16.0.0",
    "@angular/platform-browser-dynamic": "^16.0.0",
    "@angular/router": "^16.0.0"
    }

我现在将我的库安装到新的应用程序中,我遇到了以下错误...
npm错误!解析时:test-for-ui@0.0.0
npm错误!找到:@angular/animations@16.0.4 npm错误!node_modules/@angular/animations npm错误!
@angular/animations@"^16.0.0”from the root project npm ERR!npm错误!无法解析依赖项:npm ERR!peer @angular/animations@"^15.2.9”from @myfirstlibrary/angular-helpers@1.0.0 npm ERR!node_modules/@myfirstlibrary/angular-helpers npm错误!
@myfirstlibrary/angular-helpers@"*”从根项目
我从阅读Stackoverflow中知道,我可以在安装时传递--legacy-peer-deps来删除或防止这样的错误并安装我的库(当我这样做时,它可以工作),但这并不理想。我的库是在Angular 15中,我希望它能够与Angular 16或以前的Angular版本一起工作。我应该如何处理我的库对等依赖关系?我不确定Angular Dependencies是否应该低于devDependencies对象中使用的版本,或者在陈述依赖关系时,我是否应该在package.json文件中使用波浪号(~)而不是插入符号(^)。我有点迷路了,有人能给我点建议吗?

q3qa4bjr

q3qa4bjr1#

解决方案1:您的库@myfirstlibrary/angular-helpers正在使用angular material包,最安全的方法是将库升级到angular 16和angular material 16。
解决方案2:在实践中,使用angular的X.0.0版本编译的库通常与X+1.0.0版本兼容,这就是@angular/material所做的,您可以在其package.json上举个例子
浸提液:

"peerDependencies": {
        "@angular/animations": "^15.0.0 || ^16.0.0",
        "@angular/cdk": "15.2.9",
        "@angular/core": "^15.0.0 || ^16.0.0",
        "@angular/common": "^15.0.0 || ^16.0.0",
        "@angular/forms": "^15.0.0 || ^16.0.0",
        "@angular/platform-browser": "^15.0.0 || ^16.0.0",
        "rxjs": "^6.5.3 || ^7.4.0"
      }

你可以在你的libary/package.json中做类似的事情。在你的库中,你需要将@angular/material包设置为依赖项(而不是peerdependency)。

相关问题