Angular。尝试安装 ngx-cookie 时发生依赖冲突

Angular. Dependency conflict when trying to install ngx-cookie

我正在尝试安装 ngx-cookie 包,这样我就可以在我的应用程序中使用 CookieService。但是,我遇到以下依赖冲突:

$ npm install ngx-cookie --save
npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR! 
npm ERR! While resolving: angular-frontend@0.0.0
npm ERR! Found: @angular/core@13.0.3
npm ERR! node_modules/@angular/core
npm ERR!   @angular/core@"~13.0.0" from the root project
npm ERR!   peer @angular/core@">9.0.0" from ngx-cookie@5.0.2
npm ERR!   node_modules/ngx-cookie
npm ERR!     ngx-cookie@"*" from the root project
npm ERR! 
npm ERR! Could not resolve dependency:
npm ERR! peer @angular/core@"13.1.1" from @angular/common@13.1.1
npm ERR! node_modules/@angular/common
npm ERR!   peer @angular/common@">9.0.0" from ngx-cookie@5.0.2
npm ERR!   node_modules/ngx-cookie
npm ERR!     ngx-cookie@"*" 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.

我已经尝试安装 ngx-cookie-service 但我遇到了同样的错误。

我该怎么办?是Angular版本的问题吗?

这不是 angular 问题。跟npm7版本关系更大。

In previous versions of npm (4-6), peer dependencies conflicts presented a warning that versions were not compatible, but would still install dependencies without an error. npm 7 will block installations if an upstream dependency conflict is present that cannot be automatically resolved.

理想情况下你不应该像在 ngx-cookie 包中那样得到这个错误,angular peer deps defined as > 这意味着它将与大于 9 的 angular 的所有版本兼容.

根据错误日志中的建议,您可以使用 --force 标志或 --legacy-peer-deps(此行为类似于版本 4-6)。

参考:- https://github.blog/2021-02-02-npm-7-is-now-generally-available/