0

I have a problem with the ApiModule generated by Swagger, I'm only providing it once in my AppModule but somehow it is being created twice, so the application doest not start due to the constructor guard:

export class ApiModule {
    public static forRoot(configurationFactory: () => Configuration): ModuleWithProviders {
        return {
            ngModule: ApiModule,
            providers: [ { provide: Configuration, useFactory: configurationFactory } ]
        };
    }

    constructor( @Optional() @SkipSelf() parentModule: ApiModule,
                 @Optional() http: HttpClient) {
        if (parentModule) {
            throw new Error('ApiModule is already loaded. Import in your base AppModule only.');
        }
        if (!http) {
            throw new Error('You need to import the HttpClientModule in your AppModule! \n' +
            'See also https://github.com/angular/angular/issues/20575');
        }
    }
}

This seems to be caused by a feature module that has recent changes, if I remove this module from the imports array, it works properly as before. This feature module is eagerly loaded, while the rest is lazily loaded with routing.

I have spent a couple days checking imports and providers and it looks right. Also I have tried debugging it and found in the stack that the module constructor is called twice: 1) by a random unrelated component (if I remove that component another raises the error) and 2) by a wrapper module with a route to a lazily loaded module. The first call is causing the issue, because when I remove the problematic module only the second call happens and the application starts properly.

I keep messing with the debugger in Chrome Dev Tools to get to know why the ApiModule is been created twice, but it is very complicated given Angular internal machinery. Would you please give me some guidance on how can I solve or debug this issue?

Thanks in advance!

Dani

Dani P.
  • 1,073
  • 1
  • 10
  • 28

2 Answers2

1

Have you checked for ApiModule tha not imported in a provider list of another modules? If a module defined Singleton adding it to providers of a module or component cause twice calling; You could check this link

1

This can be closed, one of the internal libraries that was out of my radar was providing the ApiModule :-S

During last two years none of the clients of this library shown the problem, but after introducing a new module that imports the library and having some eager and other lazy modules caused the problem in the application.

The stack trace and debugging did not help to find the issue.

Dani P.
  • 1,073
  • 1
  • 10
  • 28