Lazy Loading on ng-cli 1.0.0 Error: BrowserModule has already been loaded.
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
You should only import BrowserModule in AppModule. I'm guessing you've declared it in your SharedModule
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
I guess that's one for angular/angular as the errors are channeled from there :)
@Perezmarc @intellix I also found this error, I want to know how to solve?
@zmkn only import BrowserModule and the animations one once (app and not shared)
Watch out to not be including browserAnimationsModule twice, Angular throws the error as "BrowserModule" is imported twice.
thanks for the answer. It was very annoying error
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
+1 same problem here. the only solution is to declare browserAnimationsModule in AppModule
If you have BrowserAnimationsModule imported at the AppModule, then remove BrowserModule
Same problem here declare browserAnimationsModule in AppModule. Not in a shared Module
Same here. Why can I not import BrowserAnimationsModule in the SharedModule?
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 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 ofadmin.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 {
}
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 i have the same problem, the lazy loading modules haven't the BrowserAnimationsModule available, but the child modules have it.
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 do you check the specifications of the own of Toastr module about the config?
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
Today had the same problem :-)
Thank you guys! :-)
Added to the SharedModule - BrowserAnimationsModule and exported :-)