Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 9 : NullInjectorError: No provider for CompilerFactory

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)
});
like image 339
Rahul Kumar Avatar asked Feb 19 '20 20:02

Rahul Kumar


2 Answers

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)
like image 141
parliament Avatar answered Sep 30 '22 00:09

parliament


In angular 9 with ivy, there are no ngFactory files anymore, you no longer need main-aot.ts, only main.ts should be necessary

platformBrowserDynamic()
  .bootstrapModule(AppModule)
  .catch(err => console.error(err));
like image 25
Caro Avatar answered Sep 29 '22 23:09

Caro