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

Lazy loaded chunk not generated when lazy route is a child of non-lazy route.

Open jdcrecur opened this issue 8 years ago • 100 comments

Versions

Angular CLI: 1.6.6
Node: 8.9.4
OS: linux x64
Angular: 5.2.2
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router, service-worker

@angular/cdk: 5.1.0
@angular/cli: 1.6.6
@angular/material: 5.1.0
@angular-devkit/build-optimizer: 0.0.42
@angular-devkit/core: 0.0.29
@angular-devkit/schematics: 0.0.52
@ngtools/json-schema: 1.1.0
@ngtools/webpack: 1.9.6
@schematics/angular: 0.1.17
typescript: 2.4.2
webpack-bundle-analyzer: 2.9.2
webpack: 3.10.0

Repro steps

  • Create an angular 5 app
  • In the main root module load 2 routes by module, but not lazy loaded.
  • In one of the child modules, lazy load subsequent modules

Observed behavior

Error thrown when loading the lazy route. When search for the chunk expected in the filesystem it is not there.

Desired behavior

The lazy loaded route is loaded via a chunk create created by webpack

Mention any other details that might be useful (optional)

SO question raised, but nobody has an answer: https://stackoverflow.com/questions/48523411/angular-5-with-angular-cli-non-lazy-loading-modules-in-the-router

Demo code of the exact issue on github: https://github.com/jdcrecur/ang5ModuleRouting

jdcrecur avatar Feb 02 '18 14:02 jdcrecur

The solution of @fromage9747 worked like a charm

gianpo86 avatar Apr 03 '18 14:04 gianpo86

core.js:1448 ERROR Error: Uncaught (in promise): TypeError: undefined is not a function
TypeError: undefined is not a function
    at Array.map (<anonymous>)
    at webpackAsyncContext (eval at ./src/$$_lazy_route_resource lazy recursive (main.bundle.js:13), <anonymous>:22:34)
    at SystemJsNgModuleLoader.loadFactory (core.js:6578)
    at SystemJsNgModuleLoader.load (core.js:6542)
    at RouterConfigLoader.loadModuleFactory (router.js:4589)
    at RouterConfigLoader.load (router.js:4569)
    at MergeMapSubscriber.eval [as project] (router.js:2061)
    at MergeMapSubscriber._tryNext (mergeMap.js:128)
    at MergeMapSubscriber._next (mergeMap.js:118)
    at MergeMapSubscriber.Subscriber.next (Subscriber.js:95)
    at Array.map (<anonymous>)
    at webpackAsyncContext (eval at ./src/$$_lazy_route_resource lazy recursive (main.bundle.js:13), <anonymous>:22:34)
    at SystemJsNgModuleLoader.loadFactory (core.js:6578)
    at SystemJsNgModuleLoader.load (core.js:6542)
    at RouterConfigLoader.loadModuleFactory (router.js:4589)
    at RouterConfigLoader.load (router.js:4569)
    at MergeMapSubscriber.eval [as project] (router.js:2061)
    at MergeMapSubscriber._tryNext (mergeMap.js:128)
    at MergeMapSubscriber._next (mergeMap.js:118)
    at MergeMapSubscriber.Subscriber.next (Subscriber.js:95)
    at resolvePromise (zone.js:814)
    at resolvePromise (zone.js:771)
    at eval (zone.js:873)
    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)

splincode avatar Apr 05 '18 14:04 splincode

@kcir3v4M Thank you, is solved

splincode avatar Apr 05 '18 14:04 splincode

The solution of @kcir3v4M worked for me, Thank you :)

nanduzz avatar Apr 09 '18 16:04 nanduzz

Just check if you have the lazy module imported into the app.module, remove it and restart ng serve. It will word like charm!!!

aiherrera avatar Apr 12 '18 13:04 aiherrera

@aiherrera it doesn't help for many of us.

martin-g avatar Apr 12 '18 13:04 martin-g

I tried all of the suggested solutions. The only thing that fixes this issue for me is using "ng serve -aot".

Ok, after restarting my OS, it looks like that "ng serve" is also working after removing the import of the lazy loaded modules from the app module.

MALIK-0 avatar Apr 12 '18 17:04 MALIK-0

@icanit Your solution worked for me.. Killed all node.exe and then open new terminal and issue ng --serve everything is good again....

foote-Greg avatar Apr 15 '18 16:04 foote-Greg

In addition to exporting the lazy paths as @kcir3v4M described we had one more thing to change. We were exporting the lazy module in a barrel file. Although the lazy path in the route pointed to the module itself and not the barrel we had to remove the export in order to make ng serve (jit) work. ng serve --aot didn't mind.

@TobiasMalikowski perhaps this helps you.

denyo avatar Apr 16 '18 06:04 denyo

@foote-Greg, @icanit yeah, it also works for me.

dominik-mrugalski avatar Apr 16 '18 09:04 dominik-mrugalski

dear ng team. this b.s. cost me half a day

is that against the rules?

atodd-geoplan avatar Apr 27 '18 15:04 atodd-geoplan

Also encountering this...

francoisvdv avatar May 04 '18 15:05 francoisvdv

anyone know why ng serve --aot as workaround works, but ng serve is not?

nch3ng avatar May 12 '18 18:05 nch3ng

Angular CLI: 6.0.1 Node: 8.11.1 Angular: 6.0.1/6.0.2

Initially, I got this error message: Error: Uncaught (in promise): TypeError: undefined is not a function TypeError: undefined is not a function

I tried several approaches mentioned here, but so far none worked, for example the one by @kcir3v4M

export const wizard = 'app/project-wizard/project-wizard.module#ProjectWizardModule'; With this, I get Uncaught (in promise): Error: Cannot find module "app/project-wizard/project-wizard.module". Error: Cannot find module "app/project-wizard/project-wizard.module".

I also tried it with ./ instead of app. I also reverted back to CLI 1.6.8, but still no success.

I finally got it working with this solution:

import { ProjectWizardModule } from './project-wizard/project-wizard.module';

{
        path: 'wizard',
        loadChildren: () => ProjectWizardModule
 },

Another workaround:

export function loadWizardModule() { return ProjectWizardModule }

{
        path: 'wizard',
        loadChildren: loadWizardModule
}

These workarounds might work for you - but they are not lazy-loading.

matthiassommer avatar May 15 '18 14:05 matthiassommer

@matthiassommer, you saved my day!! It worked for me. Thank you!! :)

manuelsanchezaponte avatar May 15 '18 18:05 manuelsanchezaponte

In other tickers for the same problem it has been explained that this won't be AOT compiled. For me the best workaround for now is to use ng serve --aot

martin-g avatar May 15 '18 19:05 martin-g

there's definitely some caching/watching issue too. sometimes i bite it and ctrl+C and re-serve... definitely seen different behaviour doing this

atodd-geoplan avatar May 16 '18 08:05 atodd-geoplan

While @matthiassommer's solution might fix the recompile error, it defeats the purpose of lazy loading. You are importing all modules while defining your routes so all those modules will have to be loaded at the beginning instead of lazy-loaded when necessary during navigation.

fvoska avatar May 16 '18 09:05 fvoska

@martin-g In my case it also only works with ng serve --aot. Will it always be like this?

lucasalmeidadev avatar May 16 '18 19:05 lucasalmeidadev

@lucasalmeida8 Hopefully it will be fixed one day!

martin-g avatar May 17 '18 11:05 martin-g