Lazy loaded chunk not generated when lazy route is a child of non-lazy route.
Versions
Angular CLI: 1.6.6
Node: 8.9.4
OS: linux x64
Angular: 5.2.2
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router, service-worker
@angular/cdk: 5.1.0
@angular/cli: 1.6.6
@angular/material: 5.1.0
@angular-devkit/build-optimizer: 0.0.42
@angular-devkit/core: 0.0.29
@angular-devkit/schematics: 0.0.52
@ngtools/json-schema: 1.1.0
@ngtools/webpack: 1.9.6
@schematics/angular: 0.1.17
typescript: 2.4.2
webpack-bundle-analyzer: 2.9.2
webpack: 3.10.0
Repro steps
- Create an angular 5 app
- In the main root module load 2 routes by module, but not lazy loaded.
- In one of the child modules, lazy load subsequent modules
Observed behavior
Error thrown when loading the lazy route. When search for the chunk expected in the filesystem it is not there.
Desired behavior
The lazy loaded route is loaded via a chunk create created by webpack
Mention any other details that might be useful (optional)
SO question raised, but nobody has an answer: https://stackoverflow.com/questions/48523411/angular-5-with-angular-cli-non-lazy-loading-modules-in-the-router
Demo code of the exact issue on github: https://github.com/jdcrecur/ang5ModuleRouting
this doesn't work in the main.ts:
`let platform = platformBrowserDynamic();
if (environment.production) { fetch('/config/get').then(response => { response.json().then(result => { Globals.Configuration = result;
platform.bootstrapModule(AppModule)
.catch(err => console.log(err));
});
});
} else { Globals.Configuration = { apiUrl: environment.ApiUrl, };
platform.bootstrapModule(AppModule)
.catch(err => console.log(err));
} `
this does: `let platform = platformBrowserDynamic();
//if (environment.production) { fetch('/config/get').then(response => { response.json().then(result => { Globals.Configuration = result;
platform.bootstrapModule(AppModule)
.catch(err => console.log(err));
});
});`
Note that the if statement is commented out.
This is Angular 6.0.2 cli or 6.0.3 cli.
It will also fail if you put in an error catch too on the fetch.
This however does work:
`if (environment.production) { fetch('/config/get').then(response => { response.json().then(result => { Globals.Configuration = result;
bootstrap();
});
});
} else { Globals.Configuration = { apiUrl: environment.ApiUrl, blobUrl: environment.blobUrl, logoUrl: environment.LogoUrl, site: environment.site, useGoogleAnalytics: environment.UseGoogleAnalytics };
bootstrap();
}
function bootstrap() { platformBrowserDynamic().bootstrapModule(AppModule) .catch(err => console.log(err)); } `
So based on that, someone is doing some hackery and if it sees more than one boostrapModule call in main.ts it freaks out and doesn't build them properly. As long as there is just one and only one bootstrapModule call it's fine.
In my case (Angular version 5.2) there was a bad order in app.module.ts. As documentation said: You have to import AppRoutingModule at the end and feature modules above. Example: imports: [ BrowserModule, FormsModule, HeroesModule, AppRoutingModule ] Here is link for more details: https://angular.io/guide/router#module-import-order-matters
+1
The order is not the problem. We use angular 6.03 and angular/cli 6.07 and have the same problem. The problem is the always new versions with webpack. They are just not tested properly before they are released.
I just arrived at this after fighting with my code for about 8 hours trying to work out why it was giving me mat-toolbar not defined issues when I had already reverted the component in question to the default "something works"...
Angular CLI: 6.0.8 Node: 8.11.2 OS: win32 x64 Angular: 6.0.5
@matthiassommer Thank you. Your solution worked for me. I had encountered the same error as you ( Error: Cannot find module...) when trying @kcir3v4M's fix.
ng -v
Angular CLI: 6.0.8
Node: 9.8.0
OS: darwin x64
Angular: 6.0.0
thxs @matthiassommer work for me 😄
@matthiassommer,new way,thx
well, it work fine with ng serve on version 1.7.4, but it doesn´t work with ng build --prod
work fine with:
...
export function loadComponentModule() { return ComponentModule; }
...
{ path: 'foo', loadChildren: loadComponentModule },
I get this error in a related, but chicken-egg situation, which is this:
I depend on a 3rd party library (angular2-token) that is a service (so you need to provide it in app.module) and a guard (that I apply in a lazy route). For example:
app.routing.ts
import { Angular2TokenService } from 'angular2-token';
export const routes: Routes = [
{ path: '', redirectTo: 'pages', pathMatch: 'full' },
{ path: 'pages', loadChildren: 'app/pages/pages.module#PagesModule',
canActivate: [Angular2TokenService] },
]
app.module.ts
import { Angular2TokenService } from 'angular2-token';
...other imports...
@NgModule({
declarations: [
AppComponent,
...other components...
],
imports: [
BrowserModule,
BrowserAnimationsModule,
ModalModule.forRoot(),
...other modules...
routing
],
providers: [
AppConfig,
Angular2TokenService,
UserService
],
bootstrap: [AppComponent]
})
export class AppModule { }
Clearly the same module (angluar2-token) is imported in both app.routing and app.module, which causes this error. But obviously I cannot remove it from app.module, as it provides a service that needs to be available globally (otherwise you get no provider error).
This worked perfectly fine in Angular4 and whatever cli version I had then (1.0.0 maybe). But
- With Angular 5, it does not work with cli 1.7.4, unless I do
ng serve --aot - With Angular 5, it does work with cli 1.6.8 and
ng serve
ngular CLI: 1.7.4
Node: 8.10.0
OS: darwin x64
Angular: 5.2.11
... animations, common, compiler, compiler-cli, core, forms
... http, platform-browser, platform-browser-dynamic
... platform-server, router
@angular/cli: 1.7.4
@angular/language-service: 4.3.4
@angular-devkit/build-optimizer: 0.3.2
@angular-devkit/core: 0.3.2
@angular-devkit/schematics: 0.3.2
@ngtools/json-schema: 1.2.0
@ngtools/webpack: 1.10.2
@schematics/angular: 0.3.2
@schematics/package-update: 0.3.2
typescript: 2.4.2
webpack: 3.11.0
UPDATE
I got this scenario to work using 1.7.4 and ng serve.
- Deleted node_modules folder and package.lock.json and then npm install
- Closed terminal window
- Opened new terminal window
- PUT BACK the node_modules folder and lock file (ie BACK FROM TRASH)
Clearly some kind of caching issue as someone suggested before.
Thanks @matthiassommer , worked for me without any compilation error by doing :
export function loadSessionStageModule() {
return SessionStageModule
}
export function loadManageEvaluationModule() {
return ManageEvaluationModule
}
export const routes: Routes = [
{
path: '',
component: FullLayoutComponent,
children: [
{
path: 'session-stage',
loadChildren: loadSessionStageModule
},
{
path: 'manage-evaluation',
loadChildren: loadManageEvaluationModule
}
],
},
{
path: 'login',
component: LoginComponent
}
];
Angular CLI: 6.0.8 Node: 8.9.4 Angular: 6.0.7
my lazy module was hiddenly imported through index.ts. Once removed works fine without aot
@AbbeYassine your example is not a lazy loaded module in the first place. Lazy loading is not whenever you use loadChildren, but only when the value of loadChildren is a string that is an import path to a module, like: loadChildren: 'app/pages/pages.module#PagesModule' in https://github.com/angular/angular-cli/issues/9488#issuecomment-402282704
@matthiassommer when you use lambda expression, it's not lazy loaded
@kcir3v4M works for me. angular cli 1.7.3
hi every one i can try to solve the issue if you share the project structure and modules code. its just related to calling components and modules in wrong way
@jdcrecur i have solved you issue in repo
@kcir3v4M Your solution worked for me with the latest CLI. I think removing the modules from the app module makes sense when they are lazy loaded but yours is easier and with less impact for me. Thank you!
@matthiassommer Your solution worked for me.
I had:
{ path: 'dashboard', loadChildren: './components/core/core.module#CoreModule' },
Changed it to:
{ path: 'dashboard', loadChildren: () => CoreModule},
No more errors and the navigation works as its supposed wo without using --aot.
Thank you!
Edit:
After this I then tried to do a ng build --prod and received this error:
ERROR in Error during template compile of 'AppRoutingModule'
Function expressions are not supported in decorators in 'ɵ0'
'ɵ0' contains the error at app/app-routing.module.ts(26,38)
Consider changing the function expression into an exported function.
I then needed to make use of @matthiassommer second workaround.
import { CoreModule } from './components/core/core.module';
export function loadCoreModule() { return CoreModule; }
{ path: 'dashboard', loadChildren: loadCoreModule},
Another edit...
I now have this issue when running the built app:
Runtime compiler is not loaded
Error: Runtime compiler is not loaded
Actually, disregard that. I was importing my lazy-loaded module from the app's NgModule. Once I removed that import, the lazy loading worked correctly.
Perhaps the error messaging itself could be improved, regardless.
It works..!!