如何处理在 Angular CLI 中安装对等依赖关系?



在尝试更新我的 Angular CLI 和 NPM 时,我发现自己处于几乎无休止的错误循环中。每次更新时,我都会遇到 WARN 消息,告诉我要安装对等依赖项(见下文),但每次安装依赖项时,我都会遇到更多的 WARN 消息。有没有更好的方法来处理这种情况,还是真的需要几个小时?

npm WARN @angular/animations@5.2.1 requires a peer of @angular/core@5.2.1 
but none is installed. You must install peer dependencies yourself.
npm WARN @angular/compiler-cli@5.1.0 requires a peer of typescript@>=2.4.2 
<2.6 but none is installed. You must install peer dependencies yourself.
npm WARN @ng-bootstrap/ng-bootstrap@1.0.0-beta.6 requires a peer of 
@angular/core@^4.0.3 but none is installed. You must install peer 
dependencies yourself.
npm WARN @ng-bootstrap/ng-bootstrap@1.0.0-beta.6 requires a peer of 
@angular/common@^4.0.3 but none is installed. You must install peer 
dependencies yourself.
npm WARN @ng-bootstrap/ng-bootstrap@1.0.0-beta.6 requires a peer of 
@angular/forms@^4.0.3 but none is installed. You must install peer 
dependencies yourself.
npm WARN @schematics/angular@0.1.17 requires a peer of @angular-
devkit/core@0.0.29 but none is installed. You must install peer dependencies 
yourself.
npm WARN @schematics/angular@0.1.17 requires a peer of @angular-
devkit/schematics@0.0.52 but none is installed. You must install peer 
dependencies yourself.
npm WARN @schematics/schematics@0.0.11 requires a peer of @angular-
devkit/core@0.0.22 but none is installed. You must install peer dependencies 
yourself.
npm WARN angular2-notifications@0.7.4 requires a peer of 
@angular/core@^4.0.1 but none is installed. You must install peer 
dependencies yourself.
npm WARN angular2-notifications@0.7.4 requires a peer of 
@angular/common@^4.0.1 but none is installed. You must install peer 
dependencies yourself.
npm WARN angular2-notifications@0.7.4 requires a peer of @angular/platform-
browser@^4.0.0 but none is installed. You must install peer dependencies 
yourself.
npm WARN angular2-notifications@0.7.4 requires a peer of 
@angular/animations@^4.0.1 but none is installed. You must install peer 
dependencies yourself.
npm WARN bootstrap@4.0.0-beta.2 requires a peer of jquery@1.9.1 - 3 but none 
is installed. You must install peer dependencies yourself.
npm WARN bootstrap@4.0.0-beta.2 requires a peer of popper.js@^1.12.3 but 
none is installed. You must install peer dependencies yourself.
npm WARN ng2-toasty@4.0.3 requires a peer of @angular/core@^2.4.7 || ^4.0.0 
but none is installed. You must install peer dependencies yourself.
npm WARN ngx-carousel@1.3.5 requires a peer of @angular/core@^2.4.0 || 
^4.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN ngx-carousel@1.3.5 requires a peer of @angular/common@^2.4.0 || 
^4.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN tsickle@0.25.5 requires a peer of typescript@>=2.4.2 <2.6 but none 
is installed. You must install peer dependencies yourself.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.1.3 
(node_modulesfsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for 
fsevents@1.1.3: wanted {"os":"darwin","arch":"any"} (current: 
{"os":"win32","arch":"x64"})

我知道我一定做错了什么,但我是Angular的新手。

对等依赖关系警告通常可以忽略。只有当对等依赖项完全缺失,或者对等依赖项的版本高于已安装的版本时,您才需要执行操作。

让我们以这个警告为例:

npm 警告 @angular/animations@5.2.1 要求对等 @angular/core@5.2.1 但未安装任何内容。您必须安装对等体 依赖自己。

使用 Angular,您希望您使用的版本在所有软件包中保持一致。如果有任何不兼容的版本,请更改package.json中的版本,然后运行npm install以便它们都同步。我倾向于将我的 Angular 版本保留在最新版本,但您需要确保您的版本对于您需要的任何 Angular 版本(可能不是最新的)都是一致的。

在这种情况下:

npm 警告 ngx-carousel@1.3.5 需要 @angular/core@^2.4.0 的对等体 || ^4.0.0 但未安装任何内容。必须安装对等依赖项 你自己。

如果您使用的是高于 4.0.0 的 Angular 版本,那么您可能不会遇到任何问题。那么这个也无事可做。如果您使用的是 2.4.0 以下的 Angular 版本,那么您需要更新您的版本。更新package.json,然后运行npm install,或针对所需的特定版本运行npm install。喜欢这个:

npm install @angular/core@5.2.3 --save

您可以省略--save如果您运行的是 npm 5.0.0 或更高版本,该版本会自动将包保存在package.json的依赖项部分中。

在这种情况下:

NPM 警告 可选 跳过可选依赖项:fsevents@1.1.3 (node_modules\fsevents):npm 警告 notsup 跳过可选依赖项: fsevents@1.1.3 不支持的平台:需要 {"OS":"Darwin","arch":"any"}(当前: {"OS":"Win32","arch":"x64"})

您正在运行Windows,并且fsevent需要OSX。可以忽略此警告。

在更新依赖项时,您可以通过将 --force 标志与 Angular cli 一起使用来忽略对等依赖项警告。

ng update @angular/cli @angular/core --force

有关选项的完整列表,请查看文档:https://angular.io/cli/update

更新您的角度(全局):

  • 通过更新:

NG 更新 @angular/CLI @angular/核心

  • 或删除 Angular 并重新安装它:


npm uninstall -g @angular/clinpm install -g @angular/cli

之后,如果你想使用你的旧的角度项目(局部):

  • 使用npm list测试某些依赖项是否已更改并收到此错误:

npm ERR! 缺少 PEER dep:mydependencie,由某些组件
需要

这意味着您必须更新您的项目:

  • 然后,您可以创建一个新项目,粘贴代码并重新安装所有配置文件。
  • 或者安装npm list所需的所有依赖项:

npm install mydependencie

我发现在Angular 项目所在的同一目录中运行npm install命令可以消除这些警告。我不知道原因。

具体来说,我正在尝试使用 ng2-completer

$ npm install ng2-completer --save
npm WARN saveError ENOENT: no such file or directory, open 'C:Workfoopackage.json'
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN enoent ENOENT: no such file or directory, open 'C:Workfoopackage.json'
npm WARN ng2-completer@3.0.3 requires a peer of @angular/common@>= 6.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN ng2-completer@3.0.3 requires a peer of @angular/core@>= 6.0.0 but noneis installed. You must install peer dependencies yourself.
npm WARN ng2-completer@3.0.3 requires a peer of @angular/forms@>= 6.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN foo No description
npm WARN foo No repository field.
npm WARN foo No README data
npm WARN foo No license field.

我无法编译。 当我再次尝试时,这次在我的 Angular 项目目录中,该目录位于 foo/foo_app 中,它工作正常。

cd foo/foo_app
$ npm install ng2-completer --save

NPM 包库中的 package.json 文件中有一个名为 peerDependencies 的部分。例如;一个用 Angular 8 构建的库,通常会将 Angular 8 列为依赖项。 对于运行低于版本 8 的任何人来说,这是一个真正的依赖项。 但是对于运行版本 8、9 或 10 的任何人来说,是否应该追究任何担忧都是值得怀疑的。

我一直安全地忽略了 Angular 更新中的这些消息,但话又说回来,我们确实有单元和赛普拉斯测试!

如上所述,这些警告通常可以忽略。但是,如果您在尝试升级时有很多第三方依赖项,这些依赖项通常会导致问题,并且也需要更新。

一个很好的资源是 npm peer,它将向您展示哪些版本可以相互配合。仅供参考,因为我在升级过程中一直在处理一堆问题。

相关内容

最新更新