Lazy loaded chunk not generated when lazy route is a child of non-lazy route.
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
The solution of @fromage9747 worked like a charm
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)
@kcir3v4M Thank you, is solved
The solution of @kcir3v4M worked for me, Thank you :)
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 it doesn't help for many of us.
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.
@icanit Your solution worked for me.. Killed all node.exe and then open new terminal and issue ng --serve everything is good again....
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.
@foote-Greg, @icanit yeah, it also works for me.
dear ng team. this b.s. cost me half a day
is that against the rules?
Also encountering this...
anyone know why ng serve --aot as workaround works, but ng serve is not?
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, you saved my day!! It worked for me. Thank you!! :)
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
there's definitely some caching/watching issue too. sometimes i bite it and ctrl+C and re-serve... definitely seen different behaviour doing this
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.
@martin-g In my case it also only works with ng serve --aot. Will it always be like this?
@lucasalmeida8 Hopefully it will be fixed one day!