将 Angular 作为依赖项添加到 npm 库的依赖项列表的正确方法

Proper way to add Angular as a dependency to the npm librarie's list of dependencies

我在 NPM 上有这个小 angular 包,我一直支持它可用于 angular 的所有新版本。在我的 package.json 中,我已将 angular 添加到 peerDependencies 的列表中,以确保它们始终存在于使用我的库的项目中:

  "peerDependencies": {
     "@angular/animations": "^7.x",
     "@angular/common": "^7.x",
     "@angular/core": "^7.x",
     "@angular/platform-browser": "^7.x",
     "rxjs": "^6.x",
     "typescript": ">=3.1.1 <3.3.0",
     "zone.js": "^0.8.x"
  }

但是随着 angular 的每个新主要版本的发布,我的库的用户在执行 npm install 时不断遇到控制台中显示的对等依赖项不匹配警​​告,如下所示:

npm WARN ng2-go-top-button@7.1.0 requires a peer of @angular/animations@^7.x but none is installed. You must install peer dependencies yourself.

所以我的问题是:在我的库中将 angular 声明为依赖项的正确方法是什么,这样我就不必在每次发布新 angular 时都更新它?也许我根本不需要 peerDependencies?但是如何确保使用我的库的项目始终具有所有需要的库?提前致谢。

好的,所以我自己想出了一个解决方案,因为我的问题的其他答案不太适合我的问题。

我本可以使用 "*""^x.x" 版本控制定义 angular 和其他依赖项,如上一个答案中所建议的那样,但这并不完全正确,因为并不总是最新版本angular 的版本与 typescript 的最新版本完全兼容,这就是为什么将依赖项版本设置为 "*" 可能会导致依赖于我的库的项目中断。

所以,最后,我决定保留我的 peerDependencies 列表 AS-IS 并在每次发布新的 angular 时更新它。这样我的库将保留兼容的依赖项列表并且不会在开发中中断,也不会导致依赖项目中断。