Repository navigation
Description
I'm submitting a ...
[ ] Regression (behavior that used to work and stopped working in a new release)
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question
Current behavior
Router fails with "Runtime compiler is not loaded" error while attempting to synchronously load an NgModule in production mode.
Expected behavior
The module is successfully loaded by the router.
Minimal reproduction of the problem with instructions
Since the issue is reproducible only in the production mode, it cannot be demoed with a plunker. Here is a minimal app generated with Angular CLI that reproduces the issue - https://github.com/antonmoiseev/router-issue.
Steps to reproduce:
- Generate an app with Angular CLI:
ng new router-issue --minimal true - Generate a new module with a single component:
ng g m home --spec false && ng g c home --spec false -it -is
- Add routing configuration for the HomeModule:
@NgModule({ imports: [ + RouterModule.forChild([ + { path: '', component: HomeComponent } + ]) ], declarations: [ HomeComponent ] }) export class HomeModule {} - Add routing configuration for the AppModule:
+export function homeModuleLoader() { + return HomeModule; +} @NgModule({ imports: [ BrowserModule, + RouterModule.forRoot([ + { path: '', loadChildren: homeModuleLoader } + ]) ], declarations: [ AppComponent ], bootstrap: [ AppComponent ] }) export class AppModule {}
- Add router outlet to the AppComponent:
@Component({ selector: 'app-root', template: ` + <h1>App component</h1> + <router-outlet></router-outlet> ` }) export class AppComponent {} - Start the development web server in the production mode:
ng serve --prod -o
- Open dev tools in the browser, you should see following error in the console:

What is the motivation / use case for changing the behavior?
I prefer creating a separate NgModule per page to better scope declarations / providers which helps maintaining the app. At the same time it makes sense to load some pages along with the main bundle (hence synchronous module loading in the router config) since users immediately face them anyway (e.g. Home page, Login page). Currently it's not possible.
Please tell us about your environment
Angular version: 4.2.6
@angular/cli: 1.2.0
node: 8.1.3
os: darwin x64
@angular/animations: 4.2.6
@angular/common: 4.2.6
@angular/compiler: 4.2.6
@angular/core: 4.2.6
@angular/forms: 4.2.6
@angular/http: 4.2.6
@angular/platform-browser: 4.2.6
@angular/platform-browser-dynamic: 4.2.6
@angular/router: 4.2.6
@angular/cli: 1.2.0
@angular/compiler-cli: 4.2.6
@angular/language-service: 4.2.6
Browser:
- [x] Chrome (desktop) version 59.0.3071.115
- [x] Firefox version 54.0.1 (64-bit)
- [x] Safari (desktop) version 10.1.1 (11603.2.5)
For Tooling issues:
- Node version: v8.1.3
- Platform: Mac
Others:
Activity
Toxicable commentedon Jul 12, 2017
Since you never reference the RuntimeCompiler it is tree shaked from the bundle by the CLI.
I don't know what this is:
but this is how you lazy load routes: https://angular.io/guide/router#lazy-loading-route-configuration
antonmoiseev commentedon Jul 12, 2017
@Toxicable I want the HomeModule to be inlined into main bundle, hence I don't use lazy loading in this app.
homeModuleLoaderis the way to synchronously load the module and make it compatible with AoT, you can read about it here - #13909.jasonaden commentedon Jul 17, 2017
@antonmoiseev You're right, but you actually need to return the NgFactory from your
homeModuleLoaderfunction. So it would be more like this:If you look at #13909 (and the issue linked to it), you'll see the fix was to make this work, but you still need to return the
NgFactory. Otherwise you would need to send down theRuntimeCompiler, which is quite large and I doubt you want to do so in a production app.yfain commentedon Aug 21, 2017
The AoT compiler was giving me "ERROR in Error encountered resolving symbol values statically. Function calls are not supported. Consider replacing the function or lambda with a reference to an exported function" while trying to do something like this:
After using the workaround suggested by @antonmoiseev the AoT went fine:
antonmoiseev commentedon Aug 21, 2017
@jasonaden I believe CLI was replacing
HomeModulewithHomeModuleNgFactoryat some point, since the factory becomes available only afterngccompiles the source code. Do you suggest it might be the CLI issue?worldspawn commentedon Jan 12, 2018
So @jasonaden you havent explained how to get a reference to this magic factory?
The really frustrating thing here is all I (and the op) wanted to do was mount a submodule to a specific path and all this seemingly unrelated rubbish has come in to play, as for some reason mounting modules to a path and lazy loading has been horrible intertwined.
glebmachine commentedon May 6, 2018
Got this error with angular 6
Toxicable commentedon May 6, 2018
@glebmachine could you provide a minimal production of the issue?
onderceylan commentedon Jul 25, 2019
In order to eagerly load modules with a function reference when AOT enabled for Angular 7, you need to create an NgFactory module as you can see from this PR - #13909.
A working solution for me was creating a clone of my module and adding
.ngfactorypostfix to the filename and also addingNgFactorypostfix to the module class name.angular-automatic-lock-bot commentedon Sep 15, 2019
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.