One or more import cycles would need to be created to compile this component, which is not supported by the current compiler configuration

Viewed 4921

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?

2 Answers

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

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

Related