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
Finally solved. Im running angular/cli
~1.7.0the cause for me was due to the fact that in app.module, I was importing a file from a barrel that was also exporting the module i was trying to lazy load.
In all, make sure all the following are done:
- Remove lazy loaded module from any barrel files.
- Do not import lazy loaded module, using es6 import statement.
- Restart angular-cli app.
For some reason, I had to kill node process and restart the app before angular-cli would work.
Fixed for me too. Potential workaround could be pointing to the router module directly.
Solved for me: I changed base href="./" to base href="/" in my index.html
The most likely cause for this problem is described by @cha0s in https://github.com/angular/angular-cli/issues/9488#issuecomment-374037802:
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.
A lazy route should only be referenced by the loadChildren string and never by an import statement. We should show a better error for this.
If you have other cases where you are getting such errors please let me know, preferably with a reproduction.
I solved this issue by removing the the lazy loaded module from an other featured module and setting loadchildren:'src/app/products.module#ProductsModule'. There should be no importing reference of lazy loaded module else where in other featured modules.
I tried everything.
- "@angular/core": "~7.2.0",
- "@angular/cli": "~7.3.1",
For me working only this:
Main routing: app-routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
const routes: Routes = [
{
path: 'TestLazy',
// <== Without 'component: xxxxxxx' and without import component and TestModule above !!!
loadChildren: './TestLazy/test.module#TestModule'
}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
Test.module with routing. Only here are import lazy components. And Children patch it's separate. Not like children: [ .... ]
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { TestLazy2Component } from './TestLazy2/TestLazy2.component';
import { TestLazyComponent } from './TestLazy.component';
const routes: Routes = [
{
path: '',
component: TestLazyComponent,
},
{
path: 'TestLazy2',
component: TestLazy2Component
}
];
@NgModule({
declarations: [
TestLazyComponent,
TestLazy2Component
],
imports: [RouterModule.forChild(routes)
],
exports: [
]
})
export class TestModule {}
in html full path
<li>
<a [routerLink]="['/TestLazy']" >Lazy Load 1</a>
</li>
<li>
<a [routerLink]="['/TestLazy/TestLazy2']">Lazy Load 2</a> // <== patch
</li>
Is this the correct lazyLoading ?? It does not matter if I click first link "TestLazy" or "TestLazy2" - is loaded "TestLazy-test-module-ngfactory.js" :)
And app.module also does not contain anything
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { AuthService } from './_services/auth.service';
import { HttpClientModule } from '@angular/common/http';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule,
HttpClientModule
],
providers: [
AuthService
],
bootstrap: [
AppComponent
]
})
export class AppModule { }
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.
Oh my god! after several hours you help me out with your response!!! Thank you!
Hi @filipesilva ,
I have strange issue, lazy loading doesn't work. It really drives me crazy.
Please take a look this, I don't know what I missed, or this is a bug.
"dependencies": {
"@agm/core": "^1.0.0-beta.5",
"@angular/animations": "^7.2.15",
"@angular/cdk": "^7.3.7",
"@angular/common": "^7.2.15",
"@angular/compiler": "^7.2.15",
"@angular/core": "^7.2.15",
"@angular/flex-layout": "7.0.0-beta.22",
"@angular/forms": "^7.2.15",
"@angular/http": "^7.2.15",
"@angular/material": "^7.3.7",
"@angular/material-moment-adapter": "^7.3.7",
"@angular/platform-browser": "^7.2.15",
"@angular/platform-browser-dynamic": "^7.2.15",
"@angular/router": "^7.2.15",
"@ngrx/effects": "^7.4.0",
"@ngrx/router-store": "^7.4.0",
"@ngrx/store": "^7.4.0",
"@ngrx/store-devtools": "^7.4.0",
"@ngx-translate/core": "^11.0.1",
"@swimlane/ngx-charts": "^10.1.0",
"@swimlane/ngx-datatable": "^14.0.0",
"@swimlane/ngx-dnd": "^7.0.0",
"@types/date-fns": "^2.6.0",
"angular-calendar": "^0.27.7",
"angular-in-memory-web-api": "^0.8.0",
"angular2-markdown": "^2.2.3",
"classlist.js": "^1.1.20150312",
"core-js": "^3.0.1",
"d3": "^5.9.2",
"date-fns": "^1.30.1",
"file-saver": "^2.0.1",
"hammerjs": "^2.0.8",
"highlight.js": "^9.15.6",
"intl": "^1.2.5",
"moment": "^2.24.0",
"ngrx-store-freeze": "^0.2.4",
"ngx-color-picker": "^7.5.0",
"ngx-cookie-service": "^2.1.0",
"ngx-drag-drop": "^2.0.0-rc.6",
"ngx-infinite-scroll": "^7.1.0",
"perfect-scrollbar": "^1.4.0",
"primeng": "^7.1.2",
"rxjs": "^6.5.1",
"rxjs-compat": "^6.5.1",
"tslib": "^1.9.0",
"web-animations-js": "2.3.1",
"zone.js": "^0.8.26"
},
"devDependencies": {
"@angular-devkit/build-angular": "^0.13.9",
"@angular/cli": "^7.3.9",
"@angular/compiler-cli": "^7.2.15",
"@angular/language-service": "^7.2.15",
"@angularclass/hmr": "^2.1.3",
"@types/file-saver": "^2.0.0",
"@types/jasmine": "^3.3.12",
"@types/jasminewd2": "^2.0.6",
"@types/node": "^12.0.0",
"codelyzer": "^5.0.1",
"jasmine-core": "^3.4.0",
"jasmine-spec-reporter": "^4.2.1",
"karma": "^4.1.0",
"karma-chrome-launcher": "^2.2.0",
"karma-cli": "^2.0.0",
"karma-coverage-istanbul-reporter": "^2.0.5",
"karma-jasmine": "^2.0.1",
"karma-jasmine-html-reporter": "^1.4.2",
"protractor": "^5.4.2",
"ts-node": "^8.1.0",
"tslint": "^5.16.0",
"typescript": "3.2.4"
}
@filipesilva , I solved the problem.
[IMPORTANT NOTICE]
"skipTemplateCodegen": true option prevents Angular 7 to build lazy loading modules, but lazy loading modules works in Angular 5 with this option.
Finally solved. Im running angular/cli
~1.7.0the cause for me was due to the fact that in app.module, I was importing a file from a barrel that was also exporting the module i was trying to lazy load.
In all, make sure all the following are done:
- Remove lazy loaded module from any barrel files.
- Do not import lazy loaded module, using es6 import statement.
- Restart angular-cli app.
For some reason, I had to kill node process and restart the app before angular-cli would work.
I forgot to remove the Module from app.modules 🤦♂️ dont be like me
Is this still a problem that people are experiencing with recent Angular CLI versions?
Yes I am still having this issue .
I was having this issue with angular-cli 8.1.3, with the (not ideal) workaround to touch app-routing.module.ts while ng-serving the app, but solved it thanks to https://github.com/angular/angular-cli/issues/10673#issuecomment-473901065 . I had a non-exported class in my app.module.ts, and exporting it solved the issue. Dunno if that's supposed to be normal, as ES modules are mostly black magic to me.
Before finding that solution, I had tracked the problem down to the generated main.js. After launching the command ng build --watch, the beginning of that file was the following, and the angular app was not starting:
(window["webpackJsonp"] = window["webpackJsonp"] || []).push([["main"],{
/***/ "./$$_lazy_route_resource lazy recursive":
/*!******************************************************!*\
!*** ./$$_lazy_route_resource lazy namespace object ***!
\******************************************************/
/*! no static exports found */
/***/ (function(module, exports) {
function webpackEmptyAsyncContext(req) {
// Here Promise.resolve().then() is used instead of new Promise() to prevent
After touching the file app-routing.module.ts , the beginning of main.js became the following (and the error was gone):
(window["webpackJsonp"] = window["webpackJsonp"] || []).push([["main"],{
/***/ "./$$_lazy_route_resource lazy recursive":
/*!******************************************************!*\
!*** ./$$_lazy_route_resource lazy namespace object ***!
\******************************************************/
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {
var map = {
"./login/login.module": [
"./src/app/login/login.module.ts",
"login-login-module"
],
"./task-process/task-process.module": [
"./src/app/task-process/task-process.module.ts",
"default~task-process-task-process-module~tasks-list-tasks-list-module",
"task-process-task-process-module"
],
"./tasks-list/tasks-list.module": [
"./src/app/tasks-list/tasks-list.module.ts",
"default~task-process-task-process-module~tasks-list-tasks-list-module",
"tasks-list-tasks-list-module"
]
};
function webpackAsyncContext(req) {
if(!__webpack_require__.o(map, req))
please do not import modules (lazy modules) in app.module.ts file
it'll reduce main.js file size and generates chunks as well
@alan-agius4 Is there any more info on this ? Recently migrated to angular 7 . Lazy loading compilation in chunks is not happening for me . I have got typescripts errors as well , but that should not be linked to lazy loading compilation right ?
@alan-agius4 Is there any more info on this ? Recently migrated to angular 7 . Lazy loading compilation in chunks is not happening for me . I have got typescripts errors as well , but that should not be linked to lazy loading compilation right ?
I would clean the project if i were you. also put namedChunks on in the build to see if they are building correctly
@ricardosaracino Thanks . I observed an issue right now after hours of debugging . The same issue was mentioned by @ceyhunn here in this exact thread : https://github.com/angular/angular/issues/27023#issuecomment-491206139 .
When I first try ng serve, I just get 6 chunks : main.js, polyfills.js, runtime.js, scripts.js, styles.js and vendor.js along with various typescript errors (I am okay with typescript errors since they are coming up as typescript has been updated from 2.4 to 3.1 and according to me they are expected and can be resolved) . What's strange is : as per @ceyhunn when I now go to the app.module.ts, where my routes are mentioned and just try : CONTROL+S by introducing some blank characters using backspace , then all the lazy loading named chunks start showing up in the terminal . I had opened an issue regarding that earlier : https://github.com/angular/angular-cli/issues/19092 which basically explains my situation with screen shots .
Do you @ricardosaracino or @ceyhunn have any idea as to what is causing this strange behavior and how it can be resolved ?
one thing to note and im not etirely sure when it was introduced is that angular started to use a angular.json file. this might be the cause of some problems.


@ricardosaracino Agreed . However I already have the angular.json file . They introduced it in angular 6 . So when I migrated the project from angular 4 to 7 , my angular-cli.json got converted into angular.json automatically .
There seems to be multiple reported issues in here which is hard to follow and hard to answer.
That said, the original reported here should not longer be reproducible in newer versions of the Angular CLI. Please update to the most recent Angular CLI version.
If the problem persists after upgrading, please open a new issue, provide a simple repository reproducing the problem, and describe the difference between the expected and current behavior.
Important to point out that Angular versions prior to 8 are no longer supported and version 8 is currently in LTS which support will terminate on the Nov 28, 2020. See https://angular.io/guide/releases#support-policy-and-schedule for more information.
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.