angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

Lazy Loading on ng-cli 1.0.0 Error: BrowserModule has already been loaded.

Open Perezmarc opened this issue 9 years ago • 35 comments

Bug Report or Feature Request (mark with an x)

- [X] bug report -> please search issues before submitting
- [ ] feature request

Versions.

@angular/cli: 1.0.0 node: 7.0.0 os: darwin x64 @angular/common: 4.0.0 @angular/compiler: 4.0.0 @angular/core: 4.0.0 @angular/forms: 4.0.0 @angular/http: 4.0.0 @angular/platform-browser: 4.0.0 @angular/platform-browser-dynamic: 4.0.0 @angular/router: 4.0.0 @angular/animations: 4.0.0 @angular/cli: 1.0.0 @angular/compiler-cli: 4.0.0

Repro steps.

Lazy Loading worked before for me in beta.26, I have updated following the guide and everything but lazy loaded modules work. When I try to render a lazy loaded module I get the following error at the console browser:

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.

In my routes module I load it like so: { path : 'p/:postId', loadChildren: './postView/postView.module#PostViewModule', canActivate : [ AuthChecker ] },

and this is how my lazy loaded module looks like: `export const lazyPostRoutes: Routes = [ { path : '', pathMatch: 'full', component: PostViewComponent, }, ];

@NgModule( { imports : [ CommonModule, SharedModule, PostElementModule, RouterModule.forChild( lazyPostRoutes ) ], declarations: [ PostViewComponent ], exports : [ PostViewComponent ] } )

export class PostViewModule { } `

The log given by the failure.

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. at y (polyfills.cabf62930fce2e0f0442.bundle.js:36) at new t (main.67dfa38472fde81d5107.bundle.js:739) at e.createInternal (4.ad08eede51bd3ba4065a.chunk.js:1) at e.t.create (main.67dfa38472fde81d5107.bundle.js:365) at t.create (main.67dfa38472fde81d5107.bundle.js:365) at e.project (main.67dfa38472fde81d5107.bundle.js:442) at e._next (main.67dfa38472fde81d5107.bundle.js:772) at e.next (main.67dfa38472fde81d5107.bundle.js:766) at main.67dfa38472fde81d5107.bundle.js:766 at t.invoke (polyfills.cabf62930fce2e0f0442.bundle.js:36) at Object.onInvoke (main.67dfa38472fde81d5107.bundle.js:365) at t.invoke (polyfills.cabf62930fce2e0f0442.bundle.js:36) at e.run (polyfills.cabf62930fce2e0f0442.bundle.js:36) at polyfills.cabf62930fce2e0f0442.bundle.js:36 at t.invokeTask (polyfills.cabf62930fce2e0f0442.bundle.js:36) at y (polyfills.cabf62930fce2e0f0442.bundle.js:36) at new t (main.67dfa38472fde81d5107.bundle.js:739) at e.createInternal (4.ad08eede51bd3ba4065a.chunk.js:1) at e.t.create (main.67dfa38472fde81d5107.bundle.js:365) at t.create (main.67dfa38472fde81d5107.bundle.js:365) at e.project (main.67dfa38472fde81d5107.bundle.js:442) at e._next (main.67dfa38472fde81d5107.bundle.js:772) at e.next (main.67dfa38472fde81d5107.bundle.js:766) at main.67dfa38472fde81d5107.bundle.js:766 at t.invoke (polyfills.cabf62930fce2e0f0442.bundle.js:36) at Object.onInvoke (main.67dfa38472fde81d5107.bundle.js:365) at t.invoke (polyfills.cabf62930fce2e0f0442.bundle.js:36) at e.run (polyfills.cabf62930fce2e0f0442.bundle.js:36) at polyfills.cabf62930fce2e0f0442.bundle.js:36 at t.invokeTask (polyfills.cabf62930fce2e0f0442.bundle.js:36) at y (polyfills.cabf62930fce2e0f0442.bundle.js:36) at l (polyfills.cabf62930fce2e0f0442.bundle.js:36) at l (polyfills.cabf62930fce2e0f0442.bundle.js:36) at polyfills.cabf62930fce2e0f0442.bundle.js:36 at t.invokeTask (polyfills.cabf62930fce2e0f0442.bundle.js:36) at Object.onInvokeTask (main.67dfa38472fde81d5107.bundle.js:365) at t.invokeTask (polyfills.cabf62930fce2e0f0442.bundle.js:36) at e.runTask (polyfills.cabf62930fce2e0f0442.bundle.js:36) at a (polyfills.cabf62930fce2e0f0442.bundle.js:36)

Desired functionality.

Load a lazy loaded module

Perezmarc avatar Mar 27 '17 09:03 Perezmarc

You should only import BrowserModule in AppModule. I'm guessing you've declared it in your SharedModule

intellix avatar Mar 27 '17 11:03 intellix

I found the error, I had browserAnimationsModule in sharedModule and in AppModule. Would've been easier if the error had shown that I had browserAnimationsModule imported twice :S

Perezmarc avatar Mar 27 '17 11:03 Perezmarc

I guess that's one for angular/angular as the errors are channeled from there :)

intellix avatar Mar 27 '17 12:03 intellix

@Perezmarc @intellix I also found this error, I want to know how to solve?

zmkn avatar Mar 29 '17 02:03 zmkn

@zmkn only import BrowserModule and the animations one once (app and not shared)

intellix avatar Mar 29 '17 07:03 intellix

Watch out to not be including browserAnimationsModule twice, Angular throws the error as "BrowserModule" is imported twice.

Perezmarc avatar Mar 29 '17 09:03 Perezmarc

thanks for the answer. It was very annoying error

akomarovsky avatar Apr 17 '17 20:04 akomarovsky

Ok the same here... i fixed moving my browserAnimationsModule from the shared module to the app.module.ts

But.. the question is.... WHY? Why cannot share on my shared module? Is any warning on official documentation? I mean... WTF Angular??? xD

alienriquebm avatar Apr 25 '17 15:04 alienriquebm

+1 same problem here. the only solution is to declare browserAnimationsModule in AppModule

gimox avatar May 16 '17 20:05 gimox

If you have BrowserAnimationsModule imported at the AppModule, then remove BrowserModule

ghost avatar May 25 '17 09:05 ghost

Same problem here declare browserAnimationsModule in AppModule. Not in a shared Module

jesseteweehi avatar May 30 '17 17:05 jesseteweehi

Same here. Why can I not import BrowserAnimationsModule in the SharedModule?

ZhiliangGong avatar Jun 03 '17 21:06 ZhiliangGong

But if you imported on app.module.ts then your child modules which feeds from shared module won't have BrowserAnimation available. How are you handling this?

rollwhistler avatar Jun 06 '17 15:06 rollwhistler

@rollwhistler why wouldn't they have BrowserAnimation ? If they are child module of AppModule it's fine, even if they import shared module. Example from my project:

admin.module.ts (main module, same as AppModule)

import { NgModule, ApplicationRef } from '@angular/core'
import { RouterModule, PreloadAllModules } from '@angular/router'
import { BrowserAnimationsModule } from '@angular/platform-browser/animations'

/*
 * Platform and Environment providers/directives/pipes
 */
import { ENV_PROVIDERS } from '../environment'
import { ROUTES } from './admin.routes'

/*
 * Guards
 */
import { AuthGuard, AdminGuard } from '../shared/guards'

/*
 * Vendor
 */
import { SimpleNotificationsModule } from 'angular2-notifications'

/*
 * Shared Modules
 */
import { GlobalSharedModule } from '../shared/shared.module'
import { AdminSharedModule } from './shared/shared.module'

/*
 * Services
 */
import { AppReadyEvent } from '../shared/components/app-ready'

/*
 * Components
 */
import { AdminComponent } from './admin.component'
import { FullscreenMenuComponent } from './shared/components'
import { NotFoundComponent } from './errors'

/*
 * Styles
 */
import '../styles/admin.scss'

@NgModule({
  bootstrap: [ AdminComponent ],
  declarations: [
    AdminComponent,
    FullscreenMenuComponent,
    NotFoundComponent
  ],
  imports: [
    BrowserAnimationsModule,
    GlobalSharedModule,
    AdminSharedModule,
    RouterModule.forRoot(ROUTES, { preloadingStrategy: PreloadAllModules }),
    SimpleNotificationsModule.forRoot()
  ],
  providers: [
    AppReadyEvent
  ]
})
export class AdminModule {
}

admin.routes.ts

// Core
import { Routes } from '@angular/router'

// Guards
import { AuthGuard, AdminGuard } from '../shared/guards'

// Components
import { AdminComponent } from './admin.component'
import { FullscreenMenuComponent } from './shared/components'
import { NotFoundComponent } from './errors'

// Routes
export const ROUTES: Routes = [
  { path: '', component: FullscreenMenuComponent, pathMatch: 'full', canActivate: [AdminGuard] },
  { path: 'webinar', loadChildren: './+webinar#WebinarModule', canActivate: [AdminGuard] },
  { path: 'auth', loadChildren: '../shared/modules/authentication#AuthenticationModule'},
  { path: '**', component: NotFoundComponent, canActivate: [AdminGuard] }
]

webinar.module.ts (child module of admin.module.ts)

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

import { GlobalSharedModule } from '../../shared/shared.module'
import { AdminSharedModule } from '../shared/shared.module'

import { routes } from './webinar.routes'
import { WebinarComponent } from './webinar.component'
import { CreateComponent } from './create'

@NgModule({
  declarations: [
    WebinarComponent,
    CreateComponent
  ],
  imports: [
    GlobalSharedModule,
    AdminSharedModule,
    RouterModule.forChild(routes)
  ],
  providers: [
  ]
})
export class WebinarModule {
}

sheerlox avatar Jun 09 '17 13:06 sheerlox

Mmmm, I'd swear my material components were not working when I moved browseranimation module import to the root module, but maybe I was doing something else wrong, I'll double check.

rollwhistler avatar Jun 09 '17 14:06 rollwhistler

@rollwhistler i have the same problem, the lazy loading modules haven't the BrowserAnimationsModule available, but the child modules have it.

AsmisAlan avatar Sep 20 '17 12:09 AsmisAlan

After moving BrowserAnimationModule to App.module.ts it is giving me an error of toastr module.

ERROR Error: Uncaught (in promise): Error: ToastrModule is already loaded. It should only be imported in your application's main module. Error: ToastrModule is already loaded. It should only be imported in your application's main module.

Does that mean we need to move all the shared module to App.module.ts ?

Sushant1791 avatar Oct 18 '17 06:10 Sushant1791

@Sushant1791 do you check the specifications of the own of Toastr module about the config?

alienriquebm avatar Oct 18 '17 15:10 alienriquebm

i got lazyload animation import solved by changing the order of the browserModule and BrowserAnimationsModule.

imports: [ BrowserModule, BrowserAnimationsModule ] thows error with lazyloding modules 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.

imports: [ BrowserAnimationsModule, BrowserModule ] works. Solved my problem

gshireesh avatar Dec 07 '17 00:12 gshireesh

Today had the same problem :-) Thank you guys! :-) Added to the SharedModule - BrowserAnimationsModule and exported :-)

nikbabchenko avatar Jan 22 '18 20:01 nikbabchenko