Angular 9 : NullInjectorError: No provider for CompilerFactory

Rahul Kumar picture Rahul Kumar · Feb 19, 2020 · Viewed 7.8k times · Source

Get the message when running the app in browser prod/aot mode. Below is my main-aot.ts

Uncaught NullInjectorError: StaticInjectorError(Platform: core)[CompilerFactory]: NullInjectorError: No provider for CompilerFactory!

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { platformBrowser } from '@angular/platform-browser';
//import { AppModuleNgFactory } from './app/app.module.ngfactory';
import { AppModule } from './app/app.module';

enableProdMode();

// tslint:disable-next-line:no-console
/*platformBrowser().bootstrapModuleFactory(AppModuleNgFactory).catch(err => {
    console.log('CANNOT LOAD AOT MODULE')
    console.dir(AppModuleNgFactory);
    console.error(err)
});*/
platformBrowser().bootstrapModule(AppModule).catch(err => {
    console.log('CANNOT LOAD AOT MODULE')
    console.dir(AppModule);
    console.error(err)
});

Answer

parliament picture parliament · Jun 27, 2020

I was getting this error because I was not running ```ngcc`` as now required with Angular 9 when Ivy is enabled. So to fix this I added the following to my package.json scripts:

"postinstall": "ngcc"

Then ran it:

npm run postinstall

Also, the following bootstrap code was sufficient, platformBrowserDynamic is not required with AOT enabled:

 platformBrowser().bootstrapModule(AppModule)