更新 Angular 6 到 7 - 依赖警告和打字稿错误

Update Angular 6 to 7 - dependency warning and typescript error

我用这个 link 将我简单的 angular 前端升级到 Angular 7:

Update to Angular 7

更新时我收到了 package.json 和 polyfills.ts 文件的几个警告:

UPDATE package.json (1313 bytes)
npm WARN @ngtools/webpack@6.1.5 requires a peer of typescript@~2.4.0 || ~2.5.0 || ~2.6.0 || ~2.7.0 || ~2.8.0 || ~2.9.0 but none is installed. You must install peer dependencies yourself.
npm WARN codelyzer@4.2.1 requires a peer of @angular/compiler@>=2.3.1 <7.0.0 || >6.0.0-beta <7.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN codelyzer@4.2.1 requires a peer of @angular/core@>=2.3.1 <7.0.0 || >6.0.0-beta <7.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN codelyzer@4.2.1 requires a peer of @angular/platform-browser-dynamic@>=2.3.1 <7.0.0 || >6.0.0-beta <7.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN codelyzer@4.2.1 requires a peer of @angular/platform-browser@>=2.3.1 <7.0.0 || >6.0.0-beta <7.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN codelyzer@4.2.1 requires a peer of @angular/common@>=2.3.1 <7.0.0 || >6.0.0-beta <7.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.4 (node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.4: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})

added 69 packages, removed 4 packages, updated 16 packages and moved 2 packages in 43.395s
UPDATE package.json (1315 bytes)
UPDATE src/polyfills.ts (3165 bytes)
npm WARN codelyzer@4.2.1 requires a peer of @angular/compiler@>=2.3.1 <7.0.0 || >6.0.0-beta <7.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN codelyzer@4.2.1 requires a peer of @angular/core@>=2.3.1 <7.0.0 || >6.0.0-beta <7.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN codelyzer@4.2.1 requires a peer of @angular/platform-browser-dynamic@>=2.3.1 <7.0.0 || >6.0.0-beta <7.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN codelyzer@4.2.1 requires a peer of @angular/platform-browser@>=2.3.1 <7.0.0 || >6.0.0-beta <7.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN codelyzer@4.2.1 requires a peer of @angular/common@>=2.3.1 <7.0.0 || >6.0.0-beta <7.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.4 (node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.4: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})

我也有来自 typescript (3.0.1) 的这个错误:

Error:Initialization error (angular 2 language service). Cannot read property 'CommandTypes' of undefined

任何人都可以建议解决这些依赖性警告和打字稿错误的解决方法吗? 我需要手动更改依赖项吗?如果是,我需要一个示例如何...

您是否删除了旧的 node_modules 文件夹?如果不是请删除它并删除 package-lock.json 如果它在那里。清理 npm 缓存 npm cache clean -f 并尝试安装所有包

问题是您没有这两个软件包的最新版本。

  1. 您需要将软件包 Codelyzer 更新到最新版本。 ng update --all 应该这样做,否则您也可以手动编辑 package.json

你可以看到here这个包的最新版本(4.5.0)已经与Angular7兼容,所以你需要使用它而不是你的4.2.1版本.

  1. 您的 @ngtools/webpack 也是如此,您需要使用最新版本(参见 here,它是 7.0.2)。很可能你的 packages.json 中没有明确包含这个,所以你可能只需要在 运行 ng update.
  2. 之前做一个 npm cache clean -f

如果这仍然不起作用,我建议您删除 node_modules 目录,这将强制 npm install 从存储库中的最新版本重建所有内容,前提是您正确更新了 package.json(手动或通过 ng update)。