1

I'm using multiple module with lazy loading concept but i'm getting given below error:

error screenshoot

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. 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.

app.module.ts

     import { NgModule } from '@angular/core';

import { AppRoutingModule } from './app.routes';
import { CookieModule } from 'ngx-cookie';
/* Modules */
import { SharedModule } from 'shared/shared.module';

/* Components */
import { AppComponent } from './app.component';
/* Models */
import { User } from './shared/models/user';

// /* Services */
import { GlobalLoader } from './core/global-loader';
import { SecurityService } from './shared/services/security.service';
import { BrowserModule } from '@angular/platform-browser';
@NgModule({
   declarations: [
       AppComponent
   ],
   entryComponents: [], // Needed to declare dynamic components
   imports: [      
       BrowserModule,
       AppRoutingModule,
       CookieModule.forRoot(),                          
       SharedModule,
   ],
  exports: [
    SharedModule
  ],
   providers: [SecurityService,
       GlobalLoader,
       User],
   bootstrap: [AppComponent]
})
export class AppModule { }

shared.module.ts

    import { NgModule } from '@angular/core';
// import { FormsModule } from '@angular/forms';
// Small sample component used for dymanic tabs/accordion

/* Components */
import { SampleContent } from './components/sample-content';
import { AccordionCardComponent } from './components/accordion-card/accordion-card.component';
import { BannerComponent } from './components/banner/banner.component';
import { HeaderComponent } from './components/header/header.component';
import { SearchComponent } from './components/search/search.component';
import { TabCardComponent } from './components/tab-card/tab-card.component';
import { TableCardComponent } from './components/table-card/table-card.component';
import { Base64Pipe } from './pipes/base64.pipe';
import { FlexLayoutModule } from '@angular/flex-layout';
import { UXStyleguideModule } from 'ux-angular-styleguide';
import { CommonModule } from '@angular/common';
import { NgxDatatableModule } from '@swimlane/ngx-datatable';
import { HttpClientModule } from '@angular/common/http';
import { FormsModule } from '@angular/forms';
// import { RouterModule } from '@angular/router';
/* Shared */
@NgModule({
    declarations: [
        Base64Pipe,
        SampleContent,
        AccordionCardComponent,
        BannerComponent,
        HeaderComponent,
        SearchComponent,
        TabCardComponent,
        TableCardComponent
    ],
    entryComponents: [], // Needed to declare dynamic components
    imports: [
        HttpClientModule,
        FormsModule,
        CommonModule,
        NgxDatatableModule,
        FlexLayoutModule,
        UXStyleguideModule],
    providers: [],
    exports: [
        HttpClientModule,
        FlexLayoutModule,
        NgxDatatableModule,
        FormsModule,
        CommonModule,
        UXStyleguideModule,
        Base64Pipe,
        SampleContent,
        AccordionCardComponent,
        BannerComponent,
        HeaderComponent,
        SearchComponent,
        TabCardComponent,
        TableCardComponent
    ]
})
export class SharedModule { }

and i have nowhere imported that browserModule but still getting error.

thanks in adavance.

ER.SHASHI TIWARI
  • 877
  • 1
  • 9
  • 24

6 Answers6

2

In app.module.ts

Import {BrowserModule} from '@angular/platform-browser';

In Shared Module:

import { CommonModule } from '@angular/common';

Remove BrowserModule from Shared. Remove CommonModule from app.module

Suresh Kumar Ariya
  • 8,140
  • 1
  • 12
  • 23
2

In my case I identified that when importing some module loadChildren in app-routing-module.ts the problem starts, I still don't know how to adjust it.

loadChildren: () => import(`./home/home.module`).then(m => m.HomeModule)

or

 loadChildren: './home/home.module#HomeModule'
1

You have to do this following steps,

1) Remove CommonModule from App.module and add into Shared.module

2) Remove BrowserModule from Shared.module and into App.module

Here's an example

 import { BrowserModule } from '@angular/platform-browser';
 import { BrowserAnimationsModule, NoopAnimationsModule } from '@angular/platform-browser/animations';
 import { FormsModule } from '@angular/forms';

// import { MaterialModule } from '@angular/material';
import { NgModule } from '@angular/core';

import { AppRoutingModule } from './app.routes';
import { CookieModule } from 'ngx-cookie';
/* Modules */
import { SharedModule } from 'shared/shared.module';

/* Components */
import { AppComponent } from './app.component';
/* Models */
import { User } from './shared/models/user';

// /* Services */
import { GlobalLoader } from './core/global-loader';
// import { SecurityService } from './services/security.service';
// import { ServiceGroupService } from './services/service-group.service';

/* Shared */
// import { Base64Pipe } from './shared/base64.pipe';
import { SecurityService } from './shared/services/security.service';
 import { BrowserModule } from '@angular/platform-browser';
// import { RouterModule } from '@angular/router';
@NgModule({
    declarations: [
        AppComponent
    ],
    entryComponents: [], // Needed to declare dynamic components
    imports: [      
        BrowserModule,
        AppRoutingModule,
        CookieModule.forRoot(),
        FormsModule,                    
        SharedModule,
    ],
    providers: [SecurityService,
        GlobalLoader,
        User],
    bootstrap: [AppComponent]
})
export class AppModule { }

and Shared.module

import { NgModule } from '@angular/core';
import { HttpClientModule } from '@angular/common/http';
import { FormsModule } from '@angular/forms';
// Small sample component used for dymanic tabs/accordion

/* Components */
import { SampleContent } from './components/sample-content';
import { AccordionCardComponent } from './components/accordion-card/accordion-card.component';
import { BannerComponent } from './components/banner/banner.component';
import { HeaderComponent } from './components/header/header.component';
import { SearchComponent } from './components/search/search.component';
import { TabCardComponent } from './components/tab-card/tab-card.component';
import { TableCardComponent } from './components/table-card/table-card.component';
import { Base64Pipe } from './pipes/base64.pipe';
import { FlexLayoutModule } from '@angular/flex-layout';
import { UXStyleguideModule } from 'ux-angular-styleguide';
import { CommonModule } from '@angular/common';
import { NgxDatatableModule } from '@swimlane/ngx-datatable';
import { HttpClientModule } from '@angular/common/http';
import { FormsModule } from '@angular/forms';
import { RouterModule } from '@angular/router';
/* Shared */
@NgModule({
    declarations: [
        Base64Pipe,
        SampleContent,
        AccordionCardComponent,
        BannerComponent,
        HeaderComponent,
        SearchComponent,
        TabCardComponent,
        TableCardComponent
    ],
    entryComponents: [], // Needed to declare dynamic components
    imports: [
        HttpClientModule,
        FormsModule,
        CommonModule,
        NgxDatatableModule,
        FlexLayoutModule,
        UXStyleguideModule],
    providers: [],
    exports: [
        FlexLayoutModule,
        NgxDatatableModule,
        // FormsModule,
        UXStyleguideModule,
        Base64Pipe,
        SampleContent,
        AccordionCardComponent,
        BannerComponent,
        HeaderComponent,
        SearchComponent,
        TabCardComponent,
        TableCardComponent
    ]
})
export class SharedModule { }
Aniket Avhad
  • 3,465
  • 2
  • 18
  • 26
1

Last line in the file shared.module.ts:

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

Remove it and the error will disappear.

1

it is because if you have used *ngFor and *ngIf in your html file you have to import import { CommonModule } from '@angular/common'; in module. so try to import it inside your module.

shubham singh
  • 392
  • 2
  • 7
0

If you're using material module make sure to remove import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

Amineze
  • 156
  • 3
  • 8