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

it works after restart by ng serve -o lol

cnscorpions avatar Jun 08 '18 05:06 cnscorpions

I'm also facing this issue with non prod non aot build, but with relative path is provided using tsconfig path

@angular-devkit/architect         0.6.3
@angular-devkit/build-angular     0.6.3
@angular-devkit/build-optimizer   0.6.3
@angular-devkit/core              0.6.3
@angular-devkit/schematics        0.6.3
@angular/cdk                      6.1.0
@angular/material                 6.1.0
@ngtools/webpack                  6.0.3
@schematics/angular               0.6.3
@schematics/update                0.6.3
rxjs                              6.2.0
typescript                        2.8.3
webpack                           4.8.3

like {path:'dest' , loadChildren:'@shared/modules#DestModule'}

abdulkareemnalband avatar Jun 18 '18 14:06 abdulkareemnalband

@abdulkareemnalband loadChildren string path does not understand baseUrl and path configuration, as far I know. That maybe a feature request, but is not this same issue at all.

michaeljota avatar Jun 18 '18 14:06 michaeljota

@michaeljota Our project is configured as multiple apps , with a shred folder containing some shared modules the loadChildren with path configuration is working with one app but not with other

abdulkareemnalband avatar Jun 18 '18 15:06 abdulkareemnalband

This issue is actually quite specific, and I think it has been misunderstood. Please, check if your issue is about creating a new lazy module, or changing the string path when the cli is serving. That's cause, if not, then you should look for your issue to get proper help. 👍

michaeljota avatar Jun 18 '18 16:06 michaeljota

As of angular/cli 6.0.5 I face an issue where : Either I get a webpack.e is not a function Either I get ./xxx.module can not be found

@sluglit solution worked.

I was importing lazy loaded modules in my AppModule. Removing them made it OK :)

Thanks buddy.

dlarr avatar Jun 26 '18 10:06 dlarr

Angular CLI: 6.0.8 Node: 8.11.3 OS: win32 x64 Angular: 6.0.7

Restarting ng serve --aot works for a while until a module is recompiled and some of my modules stop loading. I can't tell exactly what causes which module to stop working but any sort of build triggers this. I have to stop and start the build to get it working again.

Running as ng server --aot --prod works, but is slow.

Running as ng server --aot --prod --optimization=none works, and is quicker.

Running as ng server --aot --configuration=dev and adding this dev to your angular.json works best.

inside projects.<YOUR PROJECT>.architect.build.configurations

        "dev": {
          "optimization": false,
          "outputHashing": "all",
          "sourceMap": true,
          "extractCss": true,
          "namedChunks": false,
          "aot": true,
          "extractLicenses": false,
          "vendorChunk": true,
          "buildOptimizer": false
        }

inside projects.<YOUR PROJECT>.architect.serve.configurations

        "dev": {
          "browserTarget": "mbnm:build:dev"
        }

I believe that this issue has to do with the way chunks are named. After a partial build, it's losing track of them.

rkajbaf avatar Jun 29 '18 15:06 rkajbaf

It seems like this is a regression introduced somewhere in 1.7.0 (works with 1.7.0-rc.0), I didn't have the issue with [email protected] and now I have it with [email protected].

Supamiu avatar Jul 06 '18 07:07 Supamiu

@sluglit Thanks, your solution is the true solution, works fine! Thanks!

speti43 avatar Jul 06 '18 10:07 speti43

@sluglit that works for me. Thanks

naichenardi avatar Jul 19 '18 14:07 naichenardi

@sluglit you indeed have to make sure that AppRoutingModule is the last in line, that fixed my problems.

https://github.com/angular/angular-cli/issues/9825#issuecomment-390410427

Woutwo avatar Aug 02 '18 08:08 Woutwo

Hey @dkarski, can you explain this solution? This worked for me, but I really did not understand how it works.

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

I'm sorry if that's a silly question ...

RafaelFronteira avatar Aug 07 '18 12:08 RafaelFronteira

its because you just stoped lazy loading and it is loaded with rest of app

wt., 7.08.2018, 14:19 użytkownik Rafael Fronteira [email protected] napisał:

Hey @dkarski https://github.com/dkarski, can you explain this solution? This worked for me, but I really did not understand how it works.

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

I'm sorry if that's a silly question ...

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

Antoniossss avatar Aug 07 '18 12:08 Antoniossss

@Antoniossss thanks man, but have you found a solution without losing lazy loading?

RafaelFronteira avatar Aug 07 '18 12:08 RafaelFronteira

@RafaelFronteira That worked for me what @sluglit suggested:

  1. Move the order of the imports. (Move the *RoutingModule to the bottom of the list of imports)
  2. Remove the lazy-loaded modules from the NgModule.imports property list

The second one solved the problem for me.

And leave the lazy loading as it was: loadChildren: 'app/dashboard/dashboard.module#DashboardModule'

And don't use AOT in dev build, it's just a workaround, but not a solution.

speti43 avatar Aug 07 '18 13:08 speti43

@speti43 It worked for me too. Thanks a lot!

RafaelFronteira avatar Aug 07 '18 14:08 RafaelFronteira

@speti43

Remove the lazy-loaded modules from the NgModule.imports property list

This worked for me. loadChildren loads the module dynamically. No need to import the module in your app.module.

Cheers

asmith2306 avatar Aug 12 '18 14:08 asmith2306

@speti43 thanks for tips work for me.

no need for ng-cli downgrade and AOT

phutaneVinayak avatar Sep 06 '18 13:09 phutaneVinayak

The workaround by @sluglit @speti43 also worked for me running CLI v6.1.0 Few things may worth mentioning.. I tried continue using a relative path in loadChildren after remove module imports in AppModule/AppRoutingModule as well as moved routing module down. { path: 'home', loadChildren: './modules/home/home.module#HomeModule' } -- this format worked

Also, if you're on Angular 6 and needs to use a absolute path you may want to try { path: 'home', loadChildren: 'src/app/modules/home/home.module#HomeModule' } instead of only specify app at the beginning, which gave a compilation error in my case

wctiger avatar Sep 07 '18 15:09 wctiger