3

I have an old angular library and when I migrate to angular 12 and try to build my library I am getting the below error:

  projects/namespace/lin-folder/src/lib/components/alerts/alerts.component.ts:7:1
      7 @Component({
        ~~~~~~~~~~~~
      8   selector: 'app-alerts',
        ~~~~~~~~~~~~~~~~~~~~~~~~~
    ... 
     39 
        
     40 }
        ~
    The component 'AlertsComponent' is used in the template but importing it would create a cycle: projects/namespace/lin-folder//src/lib/components/header/header.component.ts -> projects/namespace/lin-folder/src/lib/components/alerts/alerts.component.ts -> projects/namespace/lin-folder//src/lib/website.module.ts -> projects/namespace/lin-folder/src/lib/components/header/header.component.ts


Did anyone face the same kind of error?

Aniruddha Das
  • 17,872
  • 16
  • 92
  • 122

2 Answers2

3

The problem was I have an interface declared in my Library main module and one of my components imported that interface. So I have to move the interface to a new file and share it both in the modules file and component file.

Below is the Module code which is shared in the component

export interface WebsiteEnvironment {. // as this in the module file and imported in one of the component, it created the problem
  restUrl?: string;
  alertDelayInSeconds?: number;
  loginUrl: string;
  allowTokenToAllDomain?: string;
}

@NgModule({
  imports: [
    ...
  ],
  declarations: [
    ...
  ],
  exports: [
    ...
  ],
  entryComponents: [
    ...
  ]
})
export class WebsiteModule {
  public static forRoot(websiteEnvironment: WebsiteEnvironment): ModuleWithProviders<WebsiteModule> {
    return {
      ngModule: WebsiteModule,
      providers: [
        ...
      ]
    };
  }

  constructor(@Optional() @SkipSelf() parentModule: WebsiteModule) {
    if (parentModule) {
      throw new Error(
        ...
    }
  }
}

Simply removed the interface. create a new file and add that interface. share the interface import in both module and component will remove the circular dependency

Aniruddha Das
  • 17,872
  • 16
  • 92
  • 122
  • 1
    This was a solution to my problem aswell. I had a variable that weas being exported in module and caused the problem. Moved that variable to another file and worker properly – Cafn Aug 05 '21 at 11:15
  • 1
    I had the same issue, because I imported one module to common module and two times in other modules. I removed two other import, and all work well. – V.Tur Jan 24 '22 at 07:46
0

I got the same error when I was building a library.

To fix it I changed in tsconfig.lib.prod.json the option of enableIvy to be true