2

I want to setup lazy loading for my modules, but there is an error I can't solve.

The error is:

core.js:15724 ERROR Error: Uncaught (in promise): Error: Cannot find module 'app/invoice-builder/invoice-builder.module' Error: Cannot find module 'app/invoice-builder/invoice-builder.module'

app-routing.module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

const routes: Routes = [
  {
    path: 'invoice-builder',
    loadChildren : 'app/invoice-builder/invoice-builder.module#InvoiceBuilderModule'
  },
  {
    path: '**',
    redirectTo: 'invoice-builder'
  }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';

import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
import { from } from 'rxjs';
import { MaterialModule } from './shared/material.module';


@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    BrowserAnimationsModule,
    MaterialModule

  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

I can't understand what happen in here. I tried twice to restart the project using npm start but, that did not work. Any help would be much appreciated.

3
  • try add your component also into declarations section. Commented Apr 10, 2019 at 14:53
  • yes i tried using ./app/invoice-builder/invoice-builder.module#InvoiceBuilderModule but not success Commented Apr 10, 2019 at 14:57
  • yes i added component into declaration as you told , but nothing happen Commented Apr 10, 2019 at 14:59

3 Answers 3

6

I had a similar issue when worked with Angular 9 and managed to fixed it by using this syntax that found in docs:

const routes: Routes = [
  {
    path: 'customers',
    loadChildren: () => import('./customers/customers.module').then(m => m.CustomersModule)
  },
  {
    path: 'orders',
    loadChildren: () => import('./orders/orders.module').then(m => m.OrdersModule)
  }
];
Sign up to request clarification or add additional context in comments.

1 Comment

Your correct! They´ve breakdown Angular on Ng11... You have to go the 'old days'
3

The issue is probably due to the way you defined your path.

From the official angular docs on Lazy Loading Modules

Notice that the lazy loading syntax uses loadChildren followed by a string that is the relative path to the module, a hash mark or #, and the module’s class name.

Try updating your path to:

loadChildren : './app/invoice-builder/invoice-builder.module#InvoiceBuilderModule'

or depending on your file structure you may not need the ./app in which case you could try

loadChildren : './invoice-builder/invoice-builder.module#InvoiceBuilderModule'

1 Comment

I modified the code as you told (loadChildren : './invoice-builder/invoice-builder.module#InvoiceBuilderModule'), afterward console errors are disappear. but there is no any output . output shold be " invoice-builder works!"
0

Update the LoadChieldren path like ,

loadChildren : './invoice-builder/invoice-builder.module#InvoiceBuilderModule'

Because the invoice-builder folder is inside the app folder. And the app-routing.module.ts is also in the same directory so update the path of module.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.