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
Can confirm this is a JIT compiler issue. serve --aot works with @angular/cli 1.7.3
I also ran into this issue on 1.7.3.
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.
Angular Version: 5.2.9 Cli Version 1.7.3
I have same error on Lazy loading (without --aot), it begins firing after some recompilation when ng serve running, and after that it shows always.
Restarting ng serve don't solve anything, but killing ALL node.exe from task manager helps for some period of time and everything works fine.
This seems like some issue with caching.
It seems 'ng update' with clean 'npm i' solved this problem for me. Have no idea why.
@benmalek3601 just tried reordering as you suggested. Same issue as before. only using --aot makes it compile correctly at this stage. At least for me, that is.
I also had to downgrade from 1.7.3 to 1.6.8 to work around this issue.
@cha0s solution worked for me
Angular Version: 5.2.9 Cli Version 1.7.3
@cha0s is right, duplicate import in app.module.ts and lazy loaded in app.routing.module.ts. Just remove your lazy-loaded module in app.module.ts and update error message please.
I now don't have any duplicates when comparing app.routing.module.ts and app.module.ts. I DID have one duplicate, removed it and did ng serve. Issue persisted. Ran ng serve --aot and the issue is gone.
@fromage9747
Basically if you are doing this
loadChildren: 'app/dashboard.module#DashboardModule'
You are already importing the module when you hit this route.
So if you import the same module (DashboardModule in this case) in app.module.ts you will get the error.
Hope this help you out.
@JoffreyHernandez Pity this wasn't my problem..... @hsuanweifu thanks for the help, I tried this, but then my catch-all route to not-found occurs for all my lazy loaded routes. I suspect to workaround that I would have to place all my separated lazy loaded routes back into the main app-routing.module.ts. Not the outcome I was after. Regardless, why on earth does --aot make it work? Anyway, hopefully, there might be some other feedback on some solutions.
I experienced the same issue with angular-cli 1.7.0 . A downgrad to 1.6.8 in package.json "devDependencies": { "@angular/cli": "~1.6.8", ...} solved the problem. Of course to lazy load a module, it must be "imported" like so: { path: 'shop', loadChildren: './shop/shop.module#ShopModule' } and NOT with import { ShopModule } from './shop/shop.module'
we solved it by detach all modules from eachother and just use the loadChildren. The app also loads much faster than before.
by detach all modules from eachother
What do you mean with this ?
So is this a confirmed regression or it is an improvement in angular-cli and our apps should be fixed ? For now I pinned it to 1.6.8
Detaching was maybe the wrong word... We've removed the submodules from the import module array and let loadChildren do the work.
And it can be an inprovement, a readable error message would make it obvious
Possible solution: replace string declarations by exportable strings. Works in my latest env (JIT and AOT)
ng -v
Angular CLI: 1.7.3
Node: 8.9.3
OS: win32 x64
Angular: 5.2.9
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router
@angular/cdk: 5.2.4
@angular/cli: 1.7.3
@angular/flex-layout: 5.0.0-beta.13
@angular/material: 5.2.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.5.3
webpack: 3.11.0
Sample
import { Routes } from '@angular/router';
export const home = 'app/features/home/home.module#HomeModule';
export const user = 'app/features/user/user.module#UserModule';
export const routes: Routes = [
{
path: '',
pathMatch: 'full',
redirectTo: 'home'
},
{
path: 'home',
loadChildren: home
},
{
path: 'user',
loadChildren: user
},
{
path: '**',
redirectTo: 'home'
}
];
@kcir3v4M Making the paths to the lazy loaded routes and the routes array exportable did not resolve the issue for me. I am not importing the lazy loaded modules in my app.module either, which I know has been the source of the problem for some. Only the downgrade to @angular/cli 1.6.8 resolves the issue for me. Seems like this is a bug that needs to be fixed by the cli team.
The solution of @kcir3v4M worked for me