angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

[Bug] cli needs to be reloaded when a lazy loaded module is added in routes (FIXED)

Open michaeljota opened this issue 8 years ago • 128 comments

Versions

Angular CLI: 1.7.1
Node: 8.9.4
OS: win32 x64
Angular: 5.2.6
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router

@angular/cli: 1.7.1
@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.1
@schematics/angular: 0.3.2
@schematics/package-update: 0.3.2
typescript: 2.5.3
webpack: 3.11.0

Repro steps

  • Run ng serve to start the application bundle
  • Create a new lazy module
  • Modify the routes in the main module to do lazy load of the new module

Observed behavior

The lazy module cannot be loaded.

Stack
null: ERROR
null: Error: Uncaught (in promise): TypeError: __webpack_require__.e is not a function
message: "Uncaught (in promise): TypeError: __webpack_require__.e is not a function
TypeError: __webpack_require__.e is not a function
    at webpackAsyncContext (eval at ./src/$$_lazy_route_resource lazy recursive (http://localhost:4200/main.bundle.js:13:1), <anonymous>:11:29)
    at SystemJsNgModuleLoader.loadAndCompile (webpack-internal:///./node_modules/@angular/core/esm5/core.js:6774:82)
    at SystemJsNgModuleLoader.load (webpack-internal:///./node_modules/@angular/core/esm5/core.js:6758:60)
    at RouterConfigLoader.loadModuleFactory (webpack-internal:///./node_modules/@angular/router/esm5/router.js:4626:122)
    at RouterConfigLoader.load (webpack-internal:///./node_modules/@angular/router/esm5/router.js:4606:52)
    at MergeMapSubscriber.eval [as project] (webpack-internal:///./node_modules/@angular/router/esm5/router.js:2098:115)
    at MergeMapSubscriber._tryNext (webpack-internal:///./node_modules/rxjs/_esm5/operators/mergeMap.js:133:27)
    at MergeMapSubscriber._next (webpack-internal:///./node_modules/rxjs/_esm5/operators/mergeMap.js:123:18)
    at MergeMapSubscriber.Subscriber.next (webpack-internal:///./node_modules/rxjs/_esm5/Subscriber.js:97:18)
    at ScalarObservable._subscribe (webpack-internal:///./node_modules/rxjs/_esm5/observable/ScalarObservable.js:53:24)"
promise: ZoneAwarePromise {__zone_symbol__state: 0, __zone_symbol__value: TypeError: __webpack_require__.e is not a function…}
rejection: TypeError: __webpack_require__.e is not a function
stack: "Error: Uncaught (in promise): TypeError: __webpack_require__.e is not a function
TypeError: __webpack_require__.e is not a function
    at webpackAsyncContext (eval at ./src/$$_lazy_route_resource lazy recursive (http://localhost:4200/main.bundle.js:13:1), <anonymous>:11:29)
    at SystemJsNgModuleLoader.loadAndCompile (webpack-internal:///./node_modules/@angular/core/esm5/core.js:6774:82)
    at SystemJsNgModuleLoader.load (webpack-internal:///./node_modules/@angular/core/esm5/core.js:6758:60)
    at RouterConfigLoader.loadModuleFactory (webpack-internal:///./node_modules/@angular/router/esm5/router.js:4626:122)
    at RouterConfigLoader.load (webpack-internal:///./node_modules/@angular/router/esm5/router.js:4606:52)
    at MergeMapSubscriber.eval [as project] (webpack-internal:///./node_modules/@angular/router/esm5/router.js:2098:115)
    at MergeMapSubscriber._tryNext (webpack-internal:///./node_modules/rxjs/_esm5/operators/mergeMap.js:133:27)
    at MergeMapSubscriber._next (webpack-internal:///./node_modules/rxjs/_esm5/operators/mergeMap.js:123:18)
    at MergeMapSubscriber.Subscriber.next (webpack-internal:///./node_modules/rxjs/_esm5/Subscriber.js:97:18)
    at ScalarObservable._subscribe (webpack-internal:///./node_modules/rxjs/_esm5/observable/ScalarObservable.js:53:24)
    at resolvePromise (webpack-internal:///./node_modules/zone.js/dist/zone.js:809:31)
    at resolvePromise (webpack-internal:///./node_modules/zone.js/dist/zone.js:775:17)
    at eval (webpack-internal:///./node_modules/zone.js/dist/zone.js:858:17)
    at ZoneDelegate.invokeTask (webpack-internal:///./node_modules/zone.js/dist/zone.js:421:31)
    at Object.onInvokeTask (webpack-internal:///./node_modules/@angular/core/esm5/core.js:4956:33)
    at ZoneDelegate.invokeTask (webpack-internal:///./node_modules/zone.js/dist/zone.js:420:36)
    at Zone.runTask (webpack-internal:///./node_modules/zone.js/dist/zone.js:188:47)
    at drainMicroTaskQueue (webpack-internal:///./node_modules/zone.js/dist/zone.js:595:35)
    at <anonymous>"
task: ZoneTask {_zone: Zone, runCount: 0, _zoneDelegates: null, …}
zone: Zone {_properties: Object, _parent: Zone, _name: "angular", …}
__proto__: Object {constructor: , name: "Error", message: "", …}

Desired behavior

The module should load without errors.

Mention any other details that might be useful (optional)

The real problem here, is that the cli does not catch up the module after the path is changed.

Current workarounds:

  • Just reloading the cli makes the lazy loaded module to work again.

I haven't try every recommended workaround, but some of them are clearly not a option if you really want those modules to be lazy loaded. This is especially true for solutions like:

import { LazyModule } from 'lazy-module';

Anything that makes the module to be bundle with the app, is not an acceptable workaround.

If you are having issues with a lazy loaded module, that is NOT about changing the route o creating another lazy loaded module into the path when the CLI is serving, then you should look a proper issue, or create a new one.

michaeljota avatar Feb 28 '18 21:02 michaeljota

@fxck Well, Now that I think of it, you might be correct. Let's hope this is fixed soon.

ish-bindra avatar Apr 09 '18 16:04 ish-bindra

Hello guys, managed to fix my problem. I was importing the ChildModule with the routes after the AppRoutingModule in AppModule. Just by swapping the order everything worked nicely.

Hope it helps ;)

EcoFreak avatar Apr 09 '18 17:04 EcoFreak

@EcoFreak what do you mean with importing the ChildModule after the AppRoutingModule? Can you provide the code example of before and after?

michaeljota avatar Apr 09 '18 19:04 michaeljota

I had the same issue with: `Angular CLI: 1.7.4 Node: 9.5.0 OS: win32 x64 Angular: 5.2.9 ... animations, common, compiler, compiler-cli, core, forms ... http, language-service, platform-browser ... platform-browser-dynamic, router

@angular/animation: 4.0.0-beta.8 @angular/cdk: 5.2.4 @angular/cli: 1.7.4 @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 ` A down-grad to angular-cli 1.7.3 solved this problem.

I confirm the suggestion of dkarski : it works also with angular-cli 1.7.4

Meziano avatar Apr 11 '18 10:04 Meziano

@michaeljota Even when downgrading the version, the problem remained in my case. The problem in my case was the order of imports of the main module. The AppRoutingModule defines the main routes and loads the remaining with loadChildren. The EquipmentsModule defined extra routes and it's imported by AppRoutingModule. Before: (AppRoutingModule was imported before EquipmentsModule)

@NgModule({
  declarations: [
    AppComponent,
    LocationsComponent,
  ],
  imports: [
    HttpClientModule,
    BrowserModule,
    FormsModule,
    LayoutModule,
    AppRoutingModule,
    EquipmentsModule, //imported after
  ],

After: (AppRoutingModule was imported after EquipmentsModule)

@NgModule({
  declarations: [
    AppComponent,
    LocationsComponent,
  ],
  imports: [
    HttpClientModule,
    BrowserModule,
    FormsModule,
    LayoutModule,
    EquipmentsModule, //imported before
    AppRoutingModule,
  ],

Hope it helps!

EcoFreak avatar Apr 11 '18 14:04 EcoFreak

@EcoFreak if you are using Lazy Loading you should never import the lazy module in your application, neither in AppModule, nor AppRoutingModule. You should only reference it by a string that points to the module class of that Lazy Module.

michaeljota avatar Apr 11 '18 14:04 michaeljota

I used @angular/cli@next : npm uninstall -g @angular/cli npm install -g @angular/cli@next

qpdian avatar Apr 12 '18 17:04 qpdian

+1

ptomaszi avatar Apr 16 '18 12:04 ptomaszi

+1 Using ng serve --aot removes the issue. IDK is it a fix or rather workaround

Antoniossss avatar Apr 19 '18 07:04 Antoniossss

I've had this problem and my solution was to change the CLI version back to 1.6.5 which meant I could still use the loadChildren: 'modulePath#moduleName'.

EddWills95 avatar Apr 20 '18 09:04 EddWills95

+1

Angular CLI: 1.7.4
Node: 8.11.1
OS: win32 x64
Angular: 5.2.9

user3323 avatar Apr 22 '18 20:04 user3323

+1

Angular CLI: 1.7.3 Node: 6.11.4 OS: win32 x64 Angular: 5.2.0

ronaldohoch avatar Apr 22 '18 21:04 ronaldohoch

After downgrading to 1.7.3 everything works again. Here.


    _                      _                 ____ _     ___
   / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
  / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
 / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
/_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
               |___/
    
Angular CLI: 1.7.3
Node: 8.9.1
OS: darwin x64
Angular: 5.2.10
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, platform-server, router
... service-worker

@angular/cdk: 5.2.4
@angular/cli: 1.7.3
@angular/flex-layout: 5.0.0-beta.14
@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.6.2
webpack: 3.11.0

Ismaestro avatar Apr 23 '18 15:04 Ismaestro

@EcoFreak As annoying and weird as it is, your solution solved the error for me! Thanks!

@michaeljota That makes sense since you don't get the benefits of lazy loading if you're importing the module in your app/app routing module, but then I have a question. If I want to have a component in the lazily-loaded module with a directive, eg. [siteTitle], how am I supposed to achieve that?

Concrete Example: Let's say I have AuthModule with a component nav-bar and I want to include it in my app like so: <app-nav-bar [siteTitle]="My Site"> ... <app-nav-bar>. How can I achieve that while following Angular's rules?

jdk339 avatar Apr 23 '18 18:04 jdk339

@jdk339 You would probably need a Layout or Shell component as a root view for your lazy modules, and change the state of the title with a service or using ngrx.

michaeljota avatar Apr 23 '18 18:04 michaeljota

I'm now in Angular CLI: 6.0.0-rc.5

And it still error (This error not happended in my Angular 4 project): Code: { path: 'dashboard', loadChildren: 'app/dashboard/dashboard.module#DashboardModule' },

ng serve => ERROR in Could not resolve module app/dashboard/dashboard.module relative to /var/www/html/test/src/app/app.routing.ts

But if code: { path: 'dashboard', loadChildren: './dashboard/dashboard.module#DashboardModule' }, ng serve => success but it error in browser ERROR RangeError: Maximum call stack size exceeded

If I use const as Dercetech said for both path above ng serve => success but it error in browser Error: Cannot find module "app/dashboard/dashboard.module"

duynb avatar Apr 24 '18 04:04 duynb

I was facing same issue. I am using latest version of Angular-CLI. I was facing issue because I forgot to add pathMatch: 'full' to my lazy loading module's route.

Kalpesh300 avatar Apr 24 '18 09:04 Kalpesh300

@duynb Can you check if your ERROR RangeError: Maximum call stack size exceeded is not about recursive components? I have face that kind of errors when a component call itself in an infinite loop. If not, just check for infinite loops in general.

michaeljota avatar Apr 24 '18 14:04 michaeljota

I think that my code is not recursive components Just a router config and a module I simplified the route to be like this, and it still happen:

const appRoutes: Routes =
[
  { path: '', loadChildren: './dashboard/dashboard.module#DashboardModule' },
];

Here is the loop:

platform-browser.js:224 Router Event: RouteConfigLoadStart
platform-browser.js:219 RouteConfigLoadStart(path: )
platform-browser.js:219 RouteConfigLoadStart {route: {…}}
platform-browser.js:224 Router Event: RouteConfigLoadEnd
platform-browser.js:219 RouteConfigLoadEnd(path: )
platform-browser.js:219 RouteConfigLoadEnd {route: {…}}
platform-browser.js:224 Router Event: RouteConfigLoadStart

duynb avatar Apr 27 '18 03:04 duynb

Just thought it might help. In my case, what caused the problem was a wrongly defined module. I created a SecuredModule and lazy load that module. But I forgot that when you create a module in cli, it will also append the Module word. But sadly this error does not popup in 1.7.x version, instead I have to downgrade to 1.6.x to be able to figure out the problem.

czetsuya avatar Apr 27 '18 13:04 czetsuya