网站搜索

typescript - Angular 编译器错误(似乎是错误的实例)


当我尝试运行“ng serve”时,我遇到了以下问题:

错误在 ./src/main.ts 模块构建失败: 错误: 检测到 Angular 编译器,但它是错误类的实例。这很可能意味着你安装了多个 @ngtools/webpack 包。你可以使用 npm ls @ngtools/webpack 来检查,然后删除多余的副本。

按照这个建议:

$ npm ls @ngtools/webpack [email C:\Daten\Projects\test -- @angular/[email -- @ngtools/[email

所以没有安装多个 webpack 版本。我尝试重新安装 @angular/cli 多次……全局安装,本地安装,在使用 npm cache clean -f 和 npm cache verify 后。我对 webpack 做了同样的操作。遗憾的是,实际上没有很多人遇到这个问题,这使得它难以解决。我的谷歌搜索没有成功,因为我能找到的只是一个将该错误翻译成中文的页面,以及这两个 SO 链接(对我来说并没有提供解决方案):

https://stackoverflow.com/search?q=Angular+Compiler+was+detected

错误:安装了多个 @ngtools/webpack 包

在 Meteor 1.6 服务器和 Angular CLI 客户端 Angular-Meteor 子项目之间共享 `node_modules`?

我的 package.json:

"dependencies": {
    "@angular/animations": "^5.2.0",
    "@angular/common": "^5.2.0",
    "@angular/compiler": "^5.2.0",
    "@angular/core": "^5.2.0",
    "@angular/forms": "^5.2.0",
    "@angular/http": "^5.2.0",
    "@angular/platform-browser": "^5.2.0",
    "@angular/platform-browser-dynamic": "^5.2.0",
    "@angular/router": "^5.2.0",
    "core-js": "^2.4.1",
    "rxjs": "^5.5.6",
    "zone.js": "^0.8.19"
  },
  "devDependencies": {
    "@angular/cli": "^1.7.2",
    "@angular/compiler-cli": "^5.2.0",
    "@angular/language-service": "^5.2.0",
    "@types/jasmine": "~2.8.3",
    "@types/jasminewd2": "~2.0.2",
    "@types/node": "~6.0.60",
    "codelyzer": "^4.0.1",
    "jasmine-core": "~2.8.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~2.0.0",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "^1.2.1",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.1.2",
    "ts-node": "~4.1.0",
    "tslint": "~5.9.1",
    "typescript": "2.6.2"
  }
}

注意:

该项目可以在另一台电脑上编译——唯一的区别是操作系统(他使用的是Windows 7,而我使用的是Windows 10)。

问候,祝你周末愉快,

梅瑟比尔

答案#1

我刚遇到同样的问题。不确定这是否与升级到 Angular CLI 1.7.2 有关(我从 1.6.1 升级的)。

这是我为了解决它所做的:

  1. 删除了项目的 node_modules 文件夹(仅运行 npm install 并没有解决问题)。
  2. 以管理员身份在 Windows 10 中打开了命令提示符(可能有些过度)。
  3. 在项目文件夹中运行了 npm install。
  4. 然后,我在 VSCode 终端中成功运行了 ng build。
  5. 然后我成功运行了ng serve。

我从评论中看到你已经尝试过其中一些,但这些步骤对我有效 :-)

"devDependencies": {
    "@angular/cli": "1.7.2",
    "@angular/compiler-cli": "5.2.7",
    "@types/node": "9.4.6",
    "codelyzer": "4.2.1",
    "ts-node": "5.0.1",
    "tslint": "5.9.1",
    "typescript": "2.4.2"
}

答案#2

终于。更新我的 全局 TypeScript 版本解决了问题。现在全局使用 tsc 2.6.2,它可以正常工作。

现在使用以下内容:

  • typescript 2.6.2(全局和本地)
  • @angular/cli 1.7.2(全局和本地)

    • webpack 3.11.0
  • node -v 8.9.1
  • npm -v 5.5.1