Angular 编译器要求 TypeScript >=2.7.2 和 <2.8.0,但找到的是 2.8.3

The Angular Compiler requires TypeScript >=2.7.2 and <2.8.0 but 2.8.3 was found instead

我的 Angular 应用程序开始出现此错误:

The Angular Compiler requires TypeScript >=2.7.2 and <2.8.0 but 2.8.3 was found instead

当我尝试将打字稿降级到正确的版本时:

npm install -g typescript@2.7.2 它说更新了 1 个包。

当我使用验证打字稿版本时 npm view typescript version 我仍然得到 2.8.3

我什至尝试使用 npm uninstall -g typescript

完全删除打字稿

但是当我再次验证打字稿版本时 npm view typescript version 我还是得到 2.8.3

正确清除打字稿并将其恢复到以前版本(例如 2.7.2)的命令是什么?

我运行正在使用 node v10.0.0 和 npm v6.0.1

当我 运行 npm list -g typescript 我看到正确的版本 2.7.2 但仍然以某种方式全局安装了版本 2.8.3

在您的项目文件夹 运行 中再次 npm install typescript@2.7.2 如此处所述:

那么应该可以了。

我做了后续步骤:

  • 删除包-lock.json;
  • npm install -g --save typescript@2.7.2;
  • npm uninstall -g --save typescript@2.9.2;
  • 在 package.json 中,第 "devDependencies" 部分将字符串更新为 "typescript": "~2.7.2".

在项目的终端 ng serve --open 中完成上述 运行(我一直在使用 IDEA 2018.1)。

我做了以下事情:

  • 手动删除文件夹node_modules
  • 手动删除文件包-lock.json
  • 文件中package.json一定要设置TypeScript的依赖为

     "typescript": "2.7.2"
    
  • 运行 npm 缓存清理 -f

  • 运行 npm 安装

对我有用。

你应该npm install typescript@'>=2.7.2 <2.8.0'。这将安装您的项目所需的正确打字稿。确保你 运行 在你的 Angular 项目中。

在 Windows 上,您应该使用双引号而不是单引号,如下所示:

npm install typescript@">=2.7.2 <2.8.0"

否则,你会得到The system cannot find the file specified.

安装“@angular/compiler-cli”:“7.0.0-beta.4”解决了这个问题。 我使用 "typescript": "3.0.3".

降级到 typescript 2.9.2 (npm install typescript@2.9.2)re-running ng update --all 仍然会产生错误(两次):

Package "@angular/compiler-cli" has an incompatible peer dependency to "typescript" (requires ">=2.7.2 <2.10", would install "3.1.3"
Verified that version 2.9.2 of typescript was in node_modules.

更新到 macOS Mojave 后遇到同样的问题(以及许多其他问题)。 通过手动删除 node_modules 和 package_lock.json 修复它,在 package.json 中从 "typescript": "~2.7.2" 更改为 "typescript": "~2.8.0"和 运行 npm 安装。

要升级,运行在终端中输入以下命令。

  • 安装最新版本的 NPM

    npm install npm@latest -g
    
  • 运行 审计

    npm audit  
    
  • 更新 NPM

    npm update
    
  • 运行 NPM 运行 脚本。

    npm start
    

现在您的编译器已准备就绪。

这只是因为在您的项目 package.json 文件中有

例如"devDependencies": {"typescript": "~2.8.3" }

并且在安装了 angular cli 的计算机中有 "typescript": "2.7.2" 版本。

您可以通过 ng -vng v 进行检查。

因此,只需打开 package.json 更新您的打字稿版本run npm install 即可。