72

I am trying to implement lazy loading but getting error as following **

ERROR Error: Uncaught (in promise): Error: BrowserModule has already been loaded. If you need access to common directives such as NgIf and NgFor from a lazy loaded module, import CommonModule instead.

**

Need Help on this. Here are my Modules

  1. Shared MOdule
@NgModule({

  declarations: [TimePipe],
  providers: [
    EmsEmployeeService,
    EmsDesignationService,
    EmsOrganizationService,
    EmsAuthService,
    AmsEmployeeService,
    AmsShiftService,
    ValidatorService,
    AmsLeaveService,
    AmsAttendanceService,
    AmsDeviceService,
    AmsOrganizationService,
    AmsAlertService,
    AmsHolidayService,
    AutoCompleteService,
    AmsTimelogsService,
    LocalStorageService
  ],
  imports: [
    HttpModule,
    ToastyModule.forRoot(),
    AgmCoreModule.forRoot({
      apiKey: 'xxxxxxxxxxxxxxxxxxxxxxxxxxx'
    }),
  ],
  exports: [
    FormsModule,
    HttpModule,
    BrowserAnimationsModule,
    RouterModule,
    MaterialModule,
    MdDatepickerModule,
    MdNativeDateModule,
    ToastyModule,
    FileUploadModule,
    NgxPaginationModule,
    NguiAutoCompleteModule,
    AgmCoreModule,
    TimePipe
  ]
})
export class SharedModule { }

2.SettingModule

 @NgModule({
  imports: [
    CommonModule,
    SharedModule,
    SettingsRoutingModule
  ],
  declarations: [
    SettingsComponent,
    ShiftsComponent,
    DevicesComponent,
    AlertsComponent,
    HolidaysComponent,
    AlterTypesComponent,
    AlterEditComponent,
    ShiftTypeNewComponent,
    DeviceLogsComponent,
    ChannelTypesComponent,
    ChannelTypeEditComponent
  ], exports: [
    SettingsComponent,
    ShiftsComponent,
    DevicesComponent,
    AlertsComponent,
    HolidaysComponent,
    AlterTypesComponent,
    AlterEditComponent,
    ShiftTypeNewComponent,
    DeviceLogsComponent,
    ChannelTypesComponent,
    ChannelTypeEditComponent,
  ]
})
export class SettingsModule { }
3.SettingRoutingModule
const settings_routes: Routes = [
  { path: '', redirectTo: 'shifts', pathMatch: 'full' },
  { path: 'shifts', component: ShiftsComponent },
  { path: 'shifts/new', component: ShiftTypeNewComponent },
  { path: 'shifts/edit/:id', component: ShiftTypeNewComponent },
  { path: 'devices', component: DevicesComponent },
  { path: 'deviceLogs', component: DeviceLogsComponent },
  { path: 'holidays', component: HolidaysComponent },
  { path: 'alerts', component: AlertsComponent },
  { path: 'alerts/types', component: AlterTypesComponent },
  { path: 'alerts/:id', component: AlterEditComponent },
  { path: 'channelTypes', component: ChannelTypesComponent },
  { path: 'channelTypes/:id', component: ChannelTypeEditComponent }
];


const routes: Routes = [
  { path: '', component: SettingsComponent, children: settings_routes }
];



@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class SettingsRoutingModule { }
  1. App-routing-module
const attdendance_routes: Routes = [
  { path: '', redirectTo: 'daily', pathMatch: 'full' },
  { path: 'monthly', component: MonthlyComponent },
  { path: 'daily', component: DailyComponent },

  { path: 'daily/:empId', component: AttendanceDetailsComponent },
  { path: 'details/:empId', component: AttendanceDetailsComponent },
  { path: 'monthly/:empId', component: AttendanceDetailsComponent },
  { path: 'leaves/:empId', component: AttendanceDetailsComponent },

  { path: 'details/:empId/apply-leave', component: ApplyLeaveComponent },
  { path: 'daily/:empId/apply-leave', component: ApplyLeaveComponent },
  { path: 'daily/:empId/attendance-logs/:ofDate', component: AttendanceLogsComponent },
  { path: 'monthly/:empId/apply-leave', component: ApplyLeaveComponent },
  { path: 'leaves/:empId/apply-leave', component: ApplyLeaveComponent },
  { path: 'leaves/new/apply', component: ApplyLeaveComponent },

  { path: 'leaves', component: LeavesComponent },
  { path: 'leave-balances', component: LeaveBalancesComponent },
  { path: 'leave-balances/:empId', component: AttendanceDetailsComponent },
  { path: 'manage-leaves', component: ManageLeavesComponent },

];



const emp_routes: Routes = [
  { path: '', redirectTo: 'list', pathMatch: 'full' },
  { path: 'list', component: EmployeeListComponent },
  { path: 'list/:id', component: EmpEditComponent },
  { path: 'designations', component: DesignationsComponent }
];



const page_routes: Routes = [
  { path: '', redirectTo: 'attendances', pathMatch: 'full' },
  { path: 'employees', component: EmployeesComponent, children: emp_routes },
  { path: 'attendances', component: AttendancesComponent, children: attdendance_routes },

  { path: 'settings', loadChildren: './pages/settings/settings.module#SettingsModule' },
];

// main routes
const routes: Routes = [
  { path: '', redirectTo: 'pages', pathMatch: 'full' },
  { path: 'login', component: LoginComponent, canActivate: [LoginGuard] },
  { path: 'pages', component: PagesComponent, canActivate: [UserGuard], children: page_routes },
  { path: 'loginViaOrg', component: OrgLoginComponent },
  { path: 'download', component: AppDownloadComponent },
  { path: '**', redirectTo: 'pages' },
];

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

5.AppModule

@NgModule({

  declarations: [
    AppComponent,
    PagesComponent,
    LoginComponent,
    EmployeesComponent,
    OrgLoginComponent,
    EmployeeListComponent,
    EmpEditComponent,
    DayEventDialogComponent,
    AttendancesComponent,
    MonthlyComponent,
    AttendanceDetailsComponent,
    DailyComponent,
    DeviceDialogComponent,
    LeaveActionDialogComponent,
    LeavesComponent,
    LeaveBalancesComponent,
    ManageLeavesComponent,
    ApplyLeaveComponent,
    ConfirmDialogComponent,
    ResetPasswordDialogComponent,
    AppDownloadComponent,
    DesignationsComponent,
    AttendanceLogsComponent,
  ],

  entryComponents: [
    DayEventDialogComponent,
    DeviceDialogComponent,
    LeaveActionDialogComponent,
    ConfirmDialogComponent,
    ResetPasswordDialogComponent
  ],

  imports: [
    BrowserModule,
    // CommonModule,
    SharedModule,
    AppRoutingModule,
    // feature modules
    // SettingsModule
  ],

  providers: [
    LoginGuard, UserGuard,
  ],

  bootstrap: [AppComponent]
})
export class AppModule { }
Community
  • 1
  • 1
Er Sushil
  • 881
  • 1
  • 8
  • 17
  • Do you get this error on initial load of `index.html` or when you navigate to `/settings` ? – Günter Zöchbauer Aug 31 '17 at 07:46
  • @GünterZöchbauer while navigating to " /settings" – Er Sushil Aug 31 '17 at 07:47
  • Are you sure **none** of all the involved modules (except `AppModule`) imports `BrowserModule`? – Günter Zöchbauer Aug 31 '17 at 07:51
  • @GünterZöchbauer I Have imported CommonModule in all modules (except AppModule) – Er Sushil Aug 31 '17 at 07:54
  • Quite weird. Sounds like a bug to me. What Angular version are you using? – Günter Zöchbauer Aug 31 '17 at 08:01
  • @GünterZöchbauer this version "@angular/animations": "4.0.3", "@angular/cdk": "^2.0.0-beta.8", "@angular/common": "4.0.3", "@angular/compiler": "4.0.3", "@angular/compiler-cli": "4.0.3", "@angular/core": "4.0.3", "@angular/forms": "4.0.3", "@angular/http": "4.0.3", "@angular/material": "^2.0.0-beta.8", "@angular/platform-browser": "4.0.3", "@angular/platform-browser-dynamic": "4.0.3", "@angular/platform-server": "4.0.3", "@angular/router": "4.0.3", – Er Sushil Aug 31 '17 at 08:03
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/153327/discussion-between-er-sushil-and-gunter-zochbauer). – Er Sushil Aug 31 '17 at 08:04
  • 3
    @GünterZöchbauer I found Solution, I was importing BrowerAnimationModule in child.modules – Er Sushil Aug 31 '17 at 09:59
  • 1
    Glad to hear you could make it work :) Thanks for the feedback! – Günter Zöchbauer Aug 31 '17 at 10:08

10 Answers10

182

Import BrowserModule, BrowserAnimationsModule, HttpModule or HttpClientModule only once, preferably in your root module.

Jota.Toledo
  • 22,272
  • 8
  • 48
  • 63
  • 20
    It works! Just a little add: if you're using both `BrowserAnimationsModule` and `BrowserModule`, they should be imported in the same module. – Alvaro Jun 18 '19 at 15:52
  • Also, for those who have NoopAnimationsModule, the same rule applies. I had to also move that module. – harmonickey Aug 05 '19 at 02:49
  • THIS...So much this. I was trying to generate a lazy module with Angular CLI 9 and the output error was this non-descriptive 'cannot read properties of undefined' error. This answer saved me from burning my room down. I owe you Jota. – bwinchester Feb 12 '20 at 15:27
  • 1
    I had to import `AppRoutingModule` at the last, that resolved my error. But could not figure out the reason for that. – Amit Chigadani Apr 16 '20 at 18:52
  • You sir saved my day. Have my upvote. Would you be so kind to explain how this is necessary? – Chund Aug 28 '20 at 10:55
  • @Alvaro Your information and the answer of Jota should have made their way into the Angular-Docs! Thanks guys. – Ilker Cat Oct 20 '20 at 13:45
8

I also got the same error and finally, after little bit struggle, I was able to fix it.

Import these mentioned modules only once(in app-module only):

BrowserModule, BrowserAnimationsModule, LazyLoadImageModule (if using it), CarouselModule (if using it), InfiniteScrollModule (if using it), HttpModule ( if using it)

user9339378
  • 89
  • 1
  • 1
  • 5
    This worked for me, but how am I supposed to use CarouselModule and others from other modules outside of AppModule now? – bboynton97 Apr 06 '20 at 04:46
8

I had the same problem and Jota.Toledo gave the correct answer and I want only extend that: please check in shared module imports any modules that related with

@angular/platform-browser/animations

and move those modules into app.module.ts

7

This error can occur if you have imported BrowseModule in some child.app module.ts also. Please make sure you import CommonModule in all modules other than app.module as it has browser modules.

3

In my case I had shared module which was importing BrowserAnimationModule. I was importing shared module in my root module. to solve this error remove all imports of BrowserAnimationModule from all modules and add it to root module.

imports: [
    AppRoutingModule,
    SharedModule,
    BrowserAnimationsModule
  ],
Santosh Kadam
  • 972
  • 9
  • 11
0
@First Import BrowerModule and in imports also first include BrowserModule import:[BrowerModule ] 

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

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

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

If we declare BrowserModule in any module other than app module(Duplicate) this error will come. the In app module if we import 10 modules or plugins, first we have to import BrowserModule at the top and declare in decorates (import:[BrowserModule ]) at the top

Federico Grandi
  • 6,048
  • 4
  • 23
  • 41
  • 1)If we declare BrowserModule in any module other than app module(Duplicate) this error will come.In app module if we import 10 modules or plugins,first we have to import BrowserModule at top and declare in decorates (import:[BrowserModule ]) at top – Brahmmeswara Rao Palepu Nov 23 '18 at 10:58
  • 1
    I've edited your answer with that explanation. Please remember to edit your questions and answer instead of just commenting, when possible :) – Federico Grandi Nov 23 '18 at 15:48
0

Include all the common modules in parent component only(or appModule only). In child module, include only Child specific modules.

Also, you might need to add schemas: [NO_ERRORS_SCHEMA], in your child modules and parent modules

Shishir Arora
  • 4,257
  • 2
  • 22
  • 30
0

If you are using multiple modules, use Browser module only once in your app module or some custom module and use CommonModule from @angular/common in custom modules.

I was getting the same error , I was trying to reuse components, directives, pipes in multiple components/modules. Instead I imported all reusable components in a core module and imported the core module in multiple components/modules.

Milo
  • 3,002
  • 9
  • 25
  • 40
Chetan Birajdar
  • 301
  • 4
  • 20
0

I solved this problem by removing BreadcrumbsModule from the shared module.

Shubham
  • 95
  • 7
0

This is how I fixed it:

The lazy module that I added into my project contained BrowserModule too.

That's when I removed it from the module, then it worked.

Jan
  • 2,954
  • 2
  • 13
  • 30
Mira
  • 45
  • 7