Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug angular 2 aot failures

I have an angular 2 app generated by ng cli.

- When I run ng build (or) ng build --prod --aot=false and serve up the page things just work fine.
- But when I try to enable aot by running ng serve --aot=true and serve up, the page breaks with multiple DI errors like below

Very hard to debug. Any idea on how to debug these issues?

EXCEPTION: No provider for Options!
error_handler.js:59 ORIGINAL STACKTRACE:
ErrorHandler.handleError @ error_handler.js:59
(anonymous) @ application_ref.js:272
webpackJsonp.679.ZoneDelegate.invoke @ zone.js:229
onInvoke @ ng_zone.js:271
webpackJsonp.679.ZoneDelegate.invoke @ zone.js:228
webpackJsonp.679.Zone.run @ zone.js:113
(anonymous) @ zone.js:509
webpackJsonp.679.ZoneDelegate.invokeTask @ zone.js:262
onInvokeTask @ ng_zone.js:262
webpackJsonp.679.ZoneDelegate.invokeTask @ zone.js:261
webpackJsonp.679.Zone.runTask @ zone.js:151
drainMicroTaskQueue @ zone.js:405

like image 656
Vik Avatar asked Apr 04 '17 22:04

Vik


People also ask

What are the ways to control AOT compilation?

When you use the Angular AOT compiler, you can control your app compilation in two ways: By providing template compiler options in the tsconfig. json file. By specifying Angular metadata.

What is AOT compiler how do you enable it?

The Angular ahead-of-time (AOT) compiler converts your Angular HTML and TypeScript code into efficient JavaScript code during the build phase before the browser downloads and runs that code. Compiling your application during the build process provides a faster rendering in the browser.

What is the time to load bootstrap in AOT?

On top of improved page load, bootstrap time is down to approx. 90 msec in AoT mode and fluctuates from ~80 msec to ~110 msec which implies a volatility of roughly 20%. Compared to the JiT compiler, that is 16 to 18 times faster.


1 Answers

Are you by any chance using: angular2-logger? I've had the exact same error and found out even though I don't actually use the Options I had to provide them for --aot to work.

Eg: In your AppModule have to following import:

import {Logger, Options} from "angular2-logger/core";

Then in you list of providers make sure you add Options:

providers: [
  Logger,
  Options // <-- this is key
]

That made the sure AOT included "Options".

Now, how did I find out? The error gives you the hint it cant find "Options". So I used the Developer Tools to look at the generated sources using the sourceMaps (I looked at main.bundle.js). There I searched for the string "Options" and the only hit gave me also the final hint:

__WEBPACK_IMPORTED_MODULE_9_angular2_logger_core__["Options"])

Now thate made me realize after looking at the docs I had to provide the Options as well.

Hope this helps. :)

like image 105
Marcel Maas Avatar answered Sep 25 '22 07:09

Marcel Maas