6

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)
});
Rahul Kumar
  • 73
  • 1
  • 9
  • 1
    you should be bootstrapping ModuleNgFactory in AOT – Andrei Feb 19 '20 at 20:45
  • 1
    Have you tried rerunning ``npm install`` – Nate T Feb 19 '20 at 20:46
  • 1
    @Andrei-- Angular 9/ivy does not produce NgFactory files. – Rahul Kumar Feb 19 '20 at 21:12
  • @NathanToulbert - yes but how will it help in this case? – Rahul Kumar Feb 19 '20 at 21:12
  • It might not (and obviously didn't in your case) but that is always my first step with issues like this. Analogous to shutting down or unplugging a computer that is slow or not working properly. It is a quick step that can't hurt as you are just reinstalling dependencies that were already installed. However, if something installed improperly or incompletely (which is sometimes the case with npm), it will save you a lot of time a d energy. – Nate T Feb 20 '20 at 16:48

5 Answers5

13

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)
parliament
  • 21,544
  • 38
  • 148
  • 238
1

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));
Caro
  • 612
  • 5
  • 10
1

I recently experienced a similar error : NullInjectorError: No provider for CompilerFactory! after on deploying my remote server. I had done an automated build which was building the dist/ using npm run build --prod changing this to ng build --prod worked for me!

Kindly check that your environments are running / building the application using similar tools.

1

Instead of adding ngcc to your postinstall script, you can try to delete node_modules or just delete the installed Angular libraries and run npm install. This might also fix this issue.

In my particular case what seemed to happen is that one of the dependencies is an Angular library, those get compiled into a folder in node_modules. After updating from Angular 10 to 11 that compilation was probably cached but should be recompiled.

So both running ngcc or deleting node_modules and reinstalling the dependencies seem to fix the issue.

Rui Marques
  • 8,567
  • 3
  • 60
  • 91
1

Edit with Angular 13, the view engine is gone, and you will no longer need to run ngcc. It should just work.

Interestingly enough, the angular compatibility compiler (ngcc) will be ran behind-the-scenes when running the cli tools, but not when using the build tools directly.

This means you will probably find that when you are doing an ng build that it works fine. However, ng run build or just using the Angular's "architect" tool directly with some sort of build tool like Bazel, then you will need to run the ngcc command yourself.

@Parliaments postinstall trick should work. But just wanted to explain it more.

Cameron
  • 2,805
  • 3
  • 31
  • 45