Ionic Lazy Loading Error
I’m encountering the following stacktrace error when I’m trying to configure Lazy Loading:
Error: Uncaught (in promise): Error: Cannot find module 'pages/home/home.module'.
Error: Cannot find module 'pages/home/home.module'.
at http://localhost:8100/build/main.js:10:9
at t.invoke (http://localhost:8100/build/polyfills.js:3:14976)
at Object.onInvoke (http://localhost:8100/build/vendor.js:5134:33)
at t.invoke (http://localhost:8100/build/polyfills.js:3:14916)
at r.run (http://localhost:8100/build/polyfills.js:3:10143)
at http://localhost:8100/build/polyfills.js:3:20242
at t.invokeTask (http://localhost:8100/build/polyfills.js:3:15660)
at Object.onInvokeTask (http://localhost:8100/build/vendor.js:5125:33)
at t.invokeTask (http://localhost:8100/build/polyfills.js:3:15581)
at r.runTask (http://localhost:8100/build/polyfills.js:3:10834)
at c (http://localhost:8100/build/polyfills.js:3:19752)
at c (http://localhost:8100/build/polyfills.js:3:19461)
at http://localhost:8100/build/polyfills.js:3:20233
at t.invokeTask (http://localhost:8100/build/polyfills.js:3:15660)
at Object.onInvokeTask (http://localhost:8100/build/vendor.js:5125:33)
at t.invokeTask (http://localhost:8100/build/polyfills.js:3:15581)
at r.runTask (http://localhost:8100/build/polyfills.js:3:10834)
at o (http://localhost:8100/build/polyfills.js:3:7894)
Configuration
Ionic Framework: 3.9.2
Ionic App Scripts: 3.2.1
Angular Core: 5.2.11
Angular Compiler CLI: 5.2.11
Node: 10.12.0
OS Platform: macOS
Navigator Platform: MacIntel
User Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36
Code Snippets
Here are some of the files:
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
const routes: Routes = [
{ path: 'home', loadChildren: "pages/home/home.module#HomePageModule" },
{ path: 'form', loadChildren: "pages/form/form.module#FormPageModule" },
{ path: '', redirectTo: '/home', pathMatch: 'full' }
];
@NgModule({
imports: [ RouterModule.forRoot(routes, {
enableTracing: true
}) ],
exports: [ RouterModule ]
})
export class AppRoutingModule { }
Things to note
- On my
app/app.module.ts
, I already don’t import the modules to be lazy-loaded. - On my
app/app.component.ts
, I use a string reference to myHomePage
module. - On my
app/app-routing.module.ts
, The routes useloadChildren
that references the filepath to my modules. - App module uses
RouterModule.forRoot
while Feature modules in thepages
folder useRouterModule.forChild
.
Not sure what else to do here, I followed the instructions for lazy-loading on the ionic documentation and I read through Angular documentation for lazy-loading.
Questions
-
How do I fix the “cannot find module” error? I know the files are there but Ionic/Angular isn’t able to find where the module files are.
-
Do I actually need to have external dependencies like angular-router-loader to enable angular to load modules from string-based file path references?
Github Repository
To reproduce the bug, please check out the repository below:
I can’t provide a Stackblitz project of it because Stackblitz doesn’t currently support projects of this type:
https://stackblitz.com/github/darrensapalo/ionic-lazy-loading-error