1

I have created one Angular custom Library which I need to use in multiple projects in different workspace. I will plan to publish Library to NPM once I test it properly locally. below is my Library code :

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { AppbuttonComponent } from './appbutton.component';
import { CRMMaterialModule } from '../../material.module';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { BrowserModule } from '@angular/platform-browser';
@NgModule({
  declarations: [AppbuttonComponent],
  imports: [
    BrowserAnimationsModule,
    CRMMaterialModule
  ],
  exports: [AppbuttonComponent]
})
export class AppbuttonModule { }

And Library Public API is like

export * from './lib/components/appbutton/appbutton.module';
export * from './lib/components/appbutton/appbutton.component';

Then I link my application (in different workspace) to my library. below is my appModule.ts file code

 imports: [
    BrowserModule,
    FormsModule,
    BrowserAnimationsModule,
    CoreModule,
    AppbuttonModule,
    RouterModule.forRoot(AppRoutes, { useHash: true })....

It builds proper but when I try to run application, it gives me below error:

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.

Can Anyone help for this ? I'm stuck in this since 3 days.

ldev
  • 51
  • 4
  • Try removing BrowserAnimationsModule from your library. – Ivan Mihaylov Aug 04 '20 at 15:22
  • I have ngClass , ngIf used in library if I don't import Browser animation module then it gives me error for MatRipple – ldev Aug 05 '20 at 05:17
  • Well do what the error says and replace the BrowserAnimationsModule in your library with the CommonModule. – Ivan Mihaylov Aug 05 '20 at 11:32
  • @IvanMihaylov : But I'm using Mat-button with ngClass in library , and for that I have to use BrowserAnimationModule - otherwise it gives me error for MatRipple – ldev Aug 08 '20 at 13:05
  • I'm having the same issue with animations in a custom library. Did you ever find a solution to this? – Will Alexander Jun 17 '21 at 08:02
  • Is this still unsolved? did anyone find a solution to this? I m still having the same problem. Can't remove the BroswerAnimation module from my lib though. – Vivek Chaudhari Apr 23 '22 at 06:38

0 Answers0