Angular AOT Build: Internal error: unknown identifier undefined

This part of the application caused the problem:

export function windowFactory(): any {
    return window;
}

providers: [{
    provide: Window,
    useFactory: windowFactory
}],

Github: This fails with AoT compiler in Angular CLI, because Window (the browser window) is an interface


I faced the same error in my Angular 6 project.

when you run "ng build --prod", read the warning messages carefully. In which file it throws warning message (for eg:dataservice.ts).

then go to that (dataService.ts) file and remove the @Injectable.

@Injectable({
providedIn: 'root'
})

Then try to build again. It works for me.


Had the same error when trying to provide Date for injection instead of an injection token.

So I did:

providers: [
    { provide: Date, useValue: new Date() }
  ]

and changed it to

providers: [
    { provide: NOW, useValue: new Date() },
  ],

where NOW is defined in the service that depends on it like this

export const NOW = new InjectionToken<Date>('now');

All documented on the Angular site.