我正在尝试遵循提前(AOT)编译器指令:
https://angular.io/guide/aot-compiler
我正在开发的项目是使用快速入门种子开发的,如下所述:
https://angular.io/guide/setup
项目一直正常没有使用AOT编译器,但是当我尝试按照说明使AOT编译器工作时,我收到以下错误(这是在Windows 8上):
C:\Users\zzzzz\Desktop\quickstart>"node_modules/.bin/ngc" -p ./src
TypeError: this.compiler.analyzeModulesAsync is not a function
at CodeGenerator.codegen (C:\Users\zzzzz\Desktop\quickstart\node_modules\@angular\compiler-cli\src\codegen.js:32:14)
at codegen (C:\Users\zzzzz\Desktop\quickstart\node_modules\@angular\compiler-cli\src\main.js:13:81)
at Object.main (C:\Users\zzzzz\Desktop\quickstart\node_modules\@angular\tsc-wrapped\src\main.js:98:16)
at main (C:\Users\zzzzz\Desktop\quickstart\node_modules\@angular\compiler-cli\src\main.js:19:16)
at Object.<anonymous> (C:\Users\zzzzz\Desktop\quickstart\node_modules\@angular\compiler-cli\src\main.js:35:5)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
Compilation failed
我的package.json文件如下所示:
{
"name": "angular-quickstart",
"version": "1.0.0",
"description": "QuickStart package.json from the documentation, supplemented with testing support",
"scripts": {
"build": "tsc -p src/",
"build:watch": "tsc -p src/ -w",
"build:e2e": "tsc -p e2e/",
"serve": "lite-server -c=bs-config.json",
"serve:e2e": "lite-server -c=bs-config.e2e.json",
"prestart": "npm run build",
"start": "concurrently \"npm run build:watch\" \"npm run serve\"",
"pree2e": "npm run build:e2e",
"e2e": "concurrently \"npm run serve:e2e\" \"npm run protractor\" --kill-others --success first",
"preprotractor": "webdriver-manager update",
"protractor": "protractor protractor.config.js",
"pretest": "npm run build",
"test": "concurrently \"npm run build:watch\" \"karma start karma.conf.js\"",
"pretest:once": "npm run build",
"test:once": "karma start karma.conf.js --single-run",
"lint": "tslint ./src/**/*.ts -t verbose"
},
"keywords": [],
"author": "",
"license": "MIT",
"dependencies": {
"@angular/common": "~4.0.0",
"@angular/compiler": "~4.0.0",
"@angular/compiler-cli": "^4.2.2",
"@angular/core": "~4.0.0",
"@angular/forms": "~4.0.0",
"@angular/http": "~4.0.0",
"@angular/platform-browser": "~4.0.0",
"@angular/platform-browser-dynamic": "~4.0.0",
"@angular/platform-server": "^4.2.2",
"@angular/router": "~4.0.0",
"@ngrx/core": "~1.2.0",
"@ngrx/store": "~2.2.2",
"angular-in-memory-web-api": "~0.3.0",
"core-js": "^2.4.1",
"rxjs": "5.0.1",
"systemjs": "0.19.40",
"zone.js": "^0.8.4"
},
"devDependencies": {
"@types/jasmine": "2.5.36",
"@types/node": "^6.0.46",
"canonical-path": "0.0.2",
"concurrently": "^3.2.0",
"jasmine-core": "~2.4.1",
"karma": "^1.3.0",
"karma-chrome-launcher": "^2.0.0",
"karma-cli": "^1.0.1",
"karma-jasmine": "^1.0.2",
"karma-jasmine-html-reporter": "^0.2.2",
"lite-server": "^2.2.2",
"lodash": "^4.16.4",
"protractor": "~4.0.14",
"rimraf": "^2.5.4",
"tslint": "^3.15.1",
"typescript": "~2.1.0"
},
"repository": {}
}
当我检查我安装的@ angular / compiler的版本时,我得到4.2.2,这似乎是最新版本:
C:\Users\zzzzz\Desktop\quickstart>npm view @angular/compiler version
4.2.2
在Github上查看源代码,我看到angular / packages / compiler / src / aot / compiler.ts中有一个名为 analyzeModulesAsync 的函数:
https://github.com/angular/angular/blob/master/packages/compiler/src/aot/compiler.ts
但是,当我在node_modules文件夹上为 analyzeModulesAsync 执行grep时,我只看到该函数的调用。我没有看到它的定义。事实上,compiler.d.ts中的类定义似乎与Github上的compiler.ts文件有几点不同。
我尝试通过删除整个node_modules文件夹并重新运行npm install来尝试“重新安装”,错误消息是相同的。
我确实在这里看到了一个类似的问题:Angular AOT failing at compilation (something with angular/compiler-cli),但我对npm版本控制知之甚少,无法知道可能出现什么问题,或者如何修复它。
关于我是否在某处篡改某些指示,或者有更深层次的工作有任何想法?
解
问题在于package.json中@ angular / compiler-cli和@ angular / platform-server的版本不匹配。一旦我将它们更改为“~4.0.0”以匹配其余的角度模块,删除了node_modules文件夹并重新运行npm install
,编译器就可以工作。
我的工作理论是,因为我在两周前克隆了快速入门,并且昨天从教程中运行了这个命令npm install @angular/compiler-cli @angular/platform-server --save
,这就是导致版本不匹配的原因。
答案 0 :(得分:1)
@Rolandus:你把它弄坏了,我刚刚克隆了快速入门,然后今天添加了一个最新的套餐。将版本号匹配到4.0.0但是只使用了波浪号〜而不是插入 ^
答案 1 :(得分:0)
问题在于@ angular / compiler-cli和其他angular软件包的版本不匹配。因此,我们建议您在应用程序中以与其他angular包相同版本的方式 nstall @ angular / compiler-cli包。