[Bug] cli needs to be reloaded when a lazy loaded module is added in routes (FIXED)
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 serveto 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.
@duynb Try to load the module in the old way, import it in AppModule, and load the component from you AppRoutingModule.
Yes, it work if I load directly the component. If so, in the big project, I need to declare all the components in the AppModule. Seem that it's not good. Hope that we can use the loadChildren module in the next versions.
Thank you.
No, that's just to discard any recursive issues. One thing you should also check, is that you ain't importing any global module in your children module.
Anyway, if you can load your features module directly, then you probably have another kind of problem.
I had the same problem then stumbled across this article: https://angular.io/guide/router#module-import-order-matters.
Also look at the example (links at the top of the article) it shows different techniques to import Modules. E.g.: HerosModule is loaded eagerly CrisisCenterModule is loaded lazily
To solve the problem:
I upgraded @angular/cli to version 6.0.1 (not 100% sure if this made any difference) I moved the order of my imports around. (Move the *RoutingModule to the bottom of the list of imports) I also removed the lazy-loaded modules from the NgModule.imports property list Using an absolute path value for 'loadChildren' still works for me: loadChildren: 'app/dashboard/dashboard.module#DashboardModule'
@dkarski saved my day! thank you!
+1000 (after few hours of attempts)))
Same problem with above, Im working witn ng/cli version 6.0.1
Shutdown and run ng serve again, it works, either for --aot mode
@sluglit again, You don't have to import module which is to be lazy loaded. Thats not at all a solution. You are no longer using lazy load.
@MohitKS5 I mentioned the following in my previous post:
I also removed the lazy-loaded modules from the NgModule.imports property list
@sluglit do you have a working public repo of this example? If not, can you do one?
That would be very helpful. Thanks.
There is already a working example https://angular.io/guide/router Example link: https://angular.io/generated/zips/router/router.zip
Does this works well when you add a new module to the router module? Because, that's what this issue is about.
@michaeljota
-
Eager Loaded Routing Modules: The modules must be defined in the NgModule.imports before your router module.
-
Lazy Loaded Routing Modules: Remove the Lazy loaded modules from NgModule.imports
Either way, just make sure that your RoutingModule is last in the list of NgModule.imports E.g.: imports: [ BrowserModule, FormsModule, HeroesModule, AppRoutingModule ],
I mean, I have it this way, still, when I'm running ng serve and add a new module, to be loaded as a lazy module, the error is still reproducible.
In my case too, --aot solves the problem but only in development mode. When I build it ng build --prod --aot the app gives same error as 1st post of issue.
As mentioned above, if you ran ng serve and the build failed but the task is still running, you fixed the error and it compiled successfully, this error still may persist so make sure you cancel the running ng serve task and run it again.
I am looking forward for more improved error messages from the CLI, like "Hey, your app has an error and I am not talking about it a single bit, go figure out or go mad."
@dkarski I've :
ERROR in Error during template compile of 'RoutingModule' Function expressions are not supported in decorators in 'ɵ0' 'ɵ0' contains the error at app/routing.module.ts(15,29) Consider changing the function expression into an exported function.
Nerver mind. I make a ng serve and it's okay.
Thx !
ng serve --aot was not working for me on Angular CLI: 6.0.3 (it was working fine on previous versions).
I Tried the following code (going up one path) and it is workign now (AOT only):
{ path: 'home', loadChildren: './../app/home/home.module#HomeModule'},
I fixed it with just restarting.