Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
michaeljota opened this issue Feb 28, 2018 · 128 comments

Comments

@michaeljota
Copy link

michaeljota commented Feb 28, 2018

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.

@seaBubble
Copy link

I have the same issue

@FirassKoubaa
Copy link

+1

1 similar comment
@emanuelet
Copy link

+1

@ironstine
Copy link

+1

Confirming that restarting 'ng serve' fixes the issue.

@michaeljota
Copy link
Author

Update: This also happen when the lazy module throws an error in the first build.

@ggarbereder
Copy link

+1

@matiasfessia
Copy link

+1 same problem here!

@FARHANE
Copy link

FARHANE commented Mar 5, 2018

+1

@NEO97online
Copy link

+1 same issue, restarting fixes it for me too

@AianaM
Copy link

AianaM commented Mar 8, 2018

+1

3 similar comments
@lowbit
Copy link

lowbit commented Mar 8, 2018

+1

@lishichao1002
Copy link

+1

@GrmpyPmpkin
Copy link

+1

@denyo
Copy link

denyo commented Mar 8, 2018

Possible duplicate of #9488.

@lazarohcm
Copy link

+1

2 similar comments
@gsbraga
Copy link

gsbraga commented Mar 8, 2018

+1

@andreihincu
Copy link

+1

@stanleyeosakul
Copy link

stanleyeosakul commented Mar 9, 2018

+1; I fixed my issue with ng serve --aot

@alexsantosfilho
Copy link

+1

1 similar comment
@krunal-thakur
Copy link

+1

@smkart
Copy link

smkart commented Mar 10, 2018

Same issue here below is the error message I am getting

typeerror

@dkarski
Copy link

dkarski commented Mar 11, 2018

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

to

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

@michaeljota
Copy link
Author

@dkarski does that work with AoT?

@NEO97online
Copy link

NEO97online commented Mar 12, 2018

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.

@karol-depka
Copy link

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?)

@michaeljota
Copy link
Author

@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.

@NEO97online
Copy link

NEO97online commented Mar 12, 2018

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

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

@michaeljota
Copy link
Author

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

Sometimes this works, something the other works. IDKW.

@rkajbaf
Copy link

rkajbaf commented Jun 29, 2018

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.

@Supamiu
Copy link

Supamiu commented Jul 6, 2018

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 cli@1.7.0-rc.0 and now I have it with cli@1.7.0.

@speti43
Copy link

speti43 commented Jul 6, 2018

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

@naichenardi
Copy link

@sluglit that works for me. Thanks

@Woutwo
Copy link

Woutwo commented Aug 2, 2018

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

#9825 (comment)

@RafaelFronteira
Copy link

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 ...

@Antoniossss
Copy link

Antoniossss commented Aug 7, 2018 via email

@RafaelFronteira
Copy link

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

@speti43
Copy link

speti43 commented Aug 7, 2018

@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.

@RafaelFronteira
Copy link

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

@asmith2306
Copy link

@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

@phutaneVinayak
Copy link

@speti43
thanks for tips work for me.

no need for ng-cli downgrade and AOT

@wctiger
Copy link

wctiger commented Sep 7, 2018

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

@dercilima
Copy link

dercilima commented Sep 21, 2018

It worked for me:
angular/angular#23412 (comment)

dkarski added a commit to applantic/lisboa-ui that referenced this issue Oct 17, 2018
@shabinpshams
Copy link

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

to

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

It works for me. Thanks. Cheers!!!

@speti43
Copy link

speti43 commented Oct 29, 2018

I recomended change
{ path: 'user-panel', loadChildren: './user-panel/user-panel.module#UserPanelModule', },
to
{ path: 'user-panel', loadChildren: () => UserPanelModule, },

It works for me. Thanks. Cheers!!!

Don't do that, prod build will fail, this is just a dirty workaround.
Correct solution is above.

var-bin added a commit to var-bin/ng-events that referenced this issue Nov 14, 2018
@alan-agius4
Copy link
Collaborator

Thanks for reporting this issue. This issue is now obsolete due to changes in the recent releases. Please update to the most recent Angular CLI version. As this should have been also addressed in #12418

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.

@nickofthyme
Copy link

I had this same issue but was not importing the lazy module in my app.module.ts nor did any of the other of the aforementioned suggested solutions resolve the error.

I was lazy loading a module that was using a custom angular component library. This library worked fine in ng5 but as soon as we upgraded to ng6, this error started occurring.

The fix was COMPLETELY unrelated to the error message, in fact, the error was terribly misleading and useless! We had to start stripping modules from our app ONE BY ONE to finally see what was causing the error to get some clue as to what the actual root cause was.

As it turns out in our custom library we were importing a few modules from an index.js (bucket) file which for whatever reason Angular compiler still can't figure out.

// This is BAD!
import { MyModule } from '../my-modules/index';

// This is GOOD?
import { MyModule } from '../my-modules/my.module';

Sounds like my case is rare given that most people were able to resolve their issue with the solutions listed above but I wanted to comment in hopes that this could help someone else.

@michaeljota
Copy link
Author

Can confirm this issue is not longer reproducible. Thanks!

@michaeljota michaeljota changed the title [Bug] cli needs to be reloaded when a lazy loaded module is added in routes [Bug] cli needs to be reloaded when a lazy loaded module is added in routes (FIXED) Feb 20, 2019
@basantkumarpogeyan
Copy link

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

to

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

thanks @

@speti43
Copy link

speti43 commented Apr 14, 2019

@basantkumarpogeyan bad idea, that will fail in prod build. That's just a workaround, not the solution.

@angular-automatic-lock-bot
Copy link

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.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 9, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests