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

same issue here, even with using AoT

18:11:52.828 core.js:1448 ERROR Error: Uncaught (in promise): TypeError: undefined is not a function TypeError: undefined is not a function at Array.map () at webpackAsyncContext (eval at ./src/$$_lazy_route_resource lazy recursive (main.bundle.js:20), :26:34) at SystemJsNgModuleLoader.loadFactory (core.js:6578) at SystemJsNgModuleLoader.load (core.js:6542) at RouterConfigLoader.loadModuleFactory (router.js:4543) at RouterConfigLoader.load (router.js:4523) at MergeMapSubscriber.eval [as project] (router.js:2015) at MergeMapSubscriber._tryNext (mergeMap.js:128) at MergeMapSubscriber._next (mergeMap.js:118) at MergeMapSubscriber.Subscriber.next (Subscriber.js:92) at Array.map () at webpackAsyncContext (eval at ./src/$$_lazy_route_resource lazy recursive (main.bundle.js:20), :26:34) at SystemJsNgModuleLoader.loadFactory (core.js:6578) at SystemJsNgModuleLoader.load (core.js:6542) at RouterConfigLoader.loadModuleFactory (router.js:4543) at RouterConfigLoader.load (router.js:4523) at MergeMapSubscriber.eval [as project] (router.js:2015) at MergeMapSubscriber._tryNext (mergeMap.js:128) at MergeMapSubscriber._next (mergeMap.js:118) at MergeMapSubscriber.Subscriber.next (Subscriber.js:92) at resolvePromise (zone.js:809) at resolvePromise (zone.js:775) at eval (zone.js:858) at ZoneDelegate.invokeTask (zone.js:421) at Object.onInvokeTask (core.js:4740) at ZoneDelegate.invokeTask (zone.js:420) at Zone.runTask (zone.js:188) at drainMicroTaskQueue (zone.js:595)

ConClavi avatar Mar 17 '18 01:03 ConClavi

+1

alan994 avatar Mar 20 '18 09:03 alan994

Had the same issue with

Angular CLI: 1.7.3
Node: 8.10.0
OS: linux x64
Angular: 5.2.1
... animations, common, compiler, compiler-cli, core, forms
... http, platform-browser, platform-browser-dynamic
... platform-server, router

@angular/cli: 1.7.3
@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

and reloading the serving made the lazy loaded modules to work.

Sakuni avatar Mar 21 '18 08:03 Sakuni

For all person how did ng serve --aot did ng build (npm run build) worked for you ?

FARHANE avatar Mar 21 '18 08:03 FARHANE

For me I was making import module for my leazy loded module so that was wrong. I delete all this declarations but I had webpack error for my own created library something like webpack error e._ is not function ( I don't remember the exactly the error). So i make ng --eject and all think work perfectly, but you must do npm run build to create production js code.

FARHANE avatar Mar 21 '18 08:03 FARHANE

+1

netlander avatar Mar 25 '18 20:03 netlander

Same issue as 9488, see @kcir3v4M 's answer:

loadChildren's value must be stored in an export const. Solves issues in both JIT and AOT and preserves lazy loading capabilities:

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'
  }
];

Dercetech avatar Mar 29 '18 14:03 Dercetech

Downgrading to @angular/cli v 1.6.8 worked for me too.

vitherain avatar Mar 31 '18 11:03 vitherain

Above solution from @Dercetech to use a const worked for me. Thanks @Dercetech !

karthikchintala64 avatar Apr 02 '18 21:04 karthikchintala64

I'm runing @angular/cli v 1.7.3 and @Dercetech's solution didn't work. Any solutions?

EcoFreak avatar Apr 03 '18 14:04 EcoFreak

Have you try to turn it off and on again?

El mar., 3 de abr. de 2018 10:53, Henrique Ferreira < [email protected]> escribió:

I'm runing @angular/cli v 1.7.3 and @Dercetech https://github.com/Dercetech's solution didn't work. Any solutions?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/angular/angular-cli/issues/9825#issuecomment-378278634, or mute the thread https://github.com/notifications/unsubscribe-auth/AKBWANuPfzCWYLbs5uf5iXp6M4DUMKDYks5tk4z8gaJpZM4SXaZz .

michaeljota avatar Apr 03 '18 15:04 michaeljota

Exporting the const with childrenLoad saved my life

Paweleksltzn avatar Apr 03 '18 20:04 Paweleksltzn

Problem reproduce: https://stackblitz.com/edit/angular-6qunub

splincode avatar Apr 04 '18 13:04 splincode

Restarting CLI solved issue

trsh avatar Apr 05 '18 09:04 trsh

Relaunching my ng serve command did solve this error. Thanks.

Bryght7 avatar Apr 06 '18 12:04 Bryght7

Relaunching ng serve also solved this error.

--aot raised a bunch of errors for me.

gustavogsimas avatar Apr 06 '18 13:04 gustavogsimas

It was working fine till morning Same error occured , relaunching ng serve didnt solve this error, Any one with solution ?

nabiinChhetri avatar Apr 08 '18 11:04 nabiinChhetri

@dkarski 's solution worked for me. Import your module import { Awesome } from './awesome/awesome.module'; and do loadChildren: () => Awesome

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

I'd be afraid that would include it in the main bundle, rendering lazy loading useless. Not sure though.

fxck avatar Apr 09 '18 13:04 fxck

Well, that too. But this is ineffective with AoT as well.

michaeljota avatar Apr 09 '18 13:04 michaeljota