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

Wooow. I have been looking for a solution for more than a day now and gshireesh got it right for me. You are the man, gshireesh. Angular can be so confusing sometimes with these non trivial errors. :-(

MPauletta avatar Jan 24 '18 17:01 MPauletta

[SOLVED] If anyone is still facing errors on shared Module then add 'CommonModule' in your SharedModule

Finally it worked for me after so many efforts!!

Rumsha001 avatar Jan 30 '18 10:01 Rumsha001

I just want to add, that you need to import every module from the @angular/platform-browser into the main application module. It is not just that you should not have them replicated, it just that you can't import any module from the @angular/platform-browser into any lazy loaded module.

michaeljota avatar Feb 22 '18 16:02 michaeljota

Should this issue not be reopened? If you're lazy loading modules you cannot have a SharedModule that is dependent on BrowserAnimationsModule.

jcroll avatar Mar 14 '18 21:03 jcroll

@jcroll That kind of modules need to be loaded just once, in the main application. You just can't import any module from @angular/platform-browser into a lazy loaded module.

If you think about it, it makes total sense, because that kind of modules have to tell the entire application how to work, and you still can use that features from lazy loaded modules, because you are *not using them directly (As far I can say).

michaeljota avatar Mar 14 '18 21:03 michaeljota

@michaeljota Correct me if I am wrong tho, if you wrote a third party module (installable thru npm) that had animations in it you'd have to instruct the end consumer to also import BrowserAnimationsModule into AppModule for your library to work properly yes?

jcroll avatar Mar 14 '18 21:03 jcroll

Yes. You have. See angular/material2 for an example of that.

michaeljota avatar Mar 14 '18 21:03 michaeljota

Yeah fair enough @michaeljota appears to be the case in the docs https://material.angular.io/guide/getting-started

Ok coolio, good enough for me. Thanks.

jcroll avatar Mar 14 '18 22:03 jcroll

What I did was add BrowserAnimationsModule to the exports array of CoreModule 's NgModule instead of doing it in SharedModule. But am a bit shocked as to how feature modules are able to access BrowserAnimationModule since it was was not exported from SharedModule

frank-emeli avatar May 01 '18 15:05 frank-emeli

There are certain modules, mainly the one in Browser, that you will need to load only once. Your application will be able to use those service widely because, for Angular all of the modules will be part of only one big module.

michaeljota avatar May 02 '18 02:05 michaeljota

i solved problem by import CommonModule in shared module( and delete BrowserModule and BrowserAnimationModule here) and import BrowserAnimationModule in app module

maksonhan avatar Aug 15 '18 09:08 maksonhan

so finally do I have to import both BrowserModule and BrowserAnimationsModule in app.module? Because no other options work.

d668 avatar Aug 31 '18 09:08 d668

What you don't have to do, is import modules that have to be imported only once in app.module (Yes, both BrowserModule and BrowserAnimationsModule have to be imported only once).

michaeljota avatar Aug 31 '18 16:08 michaeljota

browserAnimationsModule

Thanks!

A-lee-201 avatar Oct 16 '18 14:10 A-lee-201

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.