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 below is the error message I am getting

typeerror

smkart avatar Mar 10 '18 12:03 smkart

I recomended change { path: 'user-panel', loadChildren: './user-panel/user-panel.module#UserPanelModule', },

to

{ path: 'user-panel', loadChildren: () => UserPanelModule, },

dkarski avatar Mar 11 '18 06:03 dkarski

@dkarski does that work with AoT?

michaeljota avatar Mar 11 '18 11:03 michaeljota

The above solution from @dkarski fixed this for me.

However, @michaeljota it doesnt work with AoT. You can't use function expressions in decorators as of now.

NEO97online avatar Mar 12 '18 05:03 NEO97online

Czesc, @dkarski :).

Is this loadChildren: () => UserPanelModule gonna do proper lazy loading? If so, whey is the loadChildren: './user-panel/user-panel.module#UserPanelModule' so widespread (any advantages?)

karol-depka avatar Mar 12 '18 13:03 karol-depka

@karol-depka, that's not a solution, it's just a work around. The only real solution is to reload the CLI.

You only can use the string version for AoT. So if you aren't using AoT for anything, sure, you can do this. But, still, it is recommended to use the string version.

michaeljota avatar Mar 12 '18 13:03 michaeljota

For some reason, the string version stopped working for me even after reloading the CLI. This happened after I updated to @angular/[email protected] from v1.6.5...

After downgrading to v1.6.5, the strings are working fine again (still needs reloading)

NEO97online avatar Mar 12 '18 14:03 NEO97online

@hermbit Try to load the string from root. loadChildren: 'app/feature/feature.module#FeatureModule'

Sometimes this works, something the other works. IDKW.

michaeljota avatar Mar 12 '18 14:03 michaeljota

For me, that's not working either (even after restarting the CLI). It's just working if you use ng serve --aot, as said by @stanleyeosakul

pf-guilhermechiara avatar Mar 12 '18 17:03 pf-guilhermechiara

downgrade your @angular/cli to '1.6.8' and load your module like this loadChildren: 'app/modules/fleet/fleet.module#FleetModule' I am trying fix this issue from last two days and finally cli version trick worked for me

ravindranath143 avatar Mar 13 '18 06:03 ravindranath143

updated to @angular/[email protected] and restart ng serve, it works.

soraliu avatar Mar 13 '18 07:03 soraliu

Using full path: loadChildren: 'app/registration/registration.module#RegistrationModule'

and ng serve --aot resolves this issue temporarily for me.

puttputt avatar Mar 13 '18 18:03 puttputt

+1

Antditto avatar Mar 13 '18 22:03 Antditto

Same Problem with Angular 5.2.8 and CLI v1.7.3 Resolved that Issue by downgrading the CLI to v1.6.8. Alternate Workaround is using ng serve --aot suggested by @stanleyeosakul

MarvKaiser avatar Mar 14 '18 08:03 MarvKaiser

Reloading the CLI didn't work?

michaeljota avatar Mar 14 '18 11:03 michaeljota

Unfortunately it did not resolve the issue (Including re-install all node_modules and clearing the npm cache). I'll wait until the bug is fixed and then update my CLI.

MarvKaiser avatar Mar 14 '18 11:03 MarvKaiser

Either 1.7.3 + ng serve --aot or 1.6.8 + ng serve as temp fixes worked. I had to downgrade to 5.2.7 due to the issue 5.2.8 has with oidc-client.

tim-white87 avatar Mar 14 '18 22:03 tim-white87

restarting the server with ng-serve fixes my problem too.

MonirTuhin avatar Mar 15 '18 04:03 MonirTuhin

@NapkinHD +1

mingtaocao avatar Mar 15 '18 09:03 mingtaocao

Same problem here using Angular CLI 1.7.2.

I'm using ng serve --aot to fixes and now worked.

asponda avatar Mar 15 '18 17:03 asponda