Search code examples
apirestnestjs

NestJS HTTP Module needs to be imported in every feature module


In nestjs the http module needs to be imported into every feature module. Is there any way to import the http module only once throughout the full application?

Although in all the feature modules the http configurations are the same, why do we need to import and configure in each one.

Thanks.


Solution

  • You could make a global module that imports it and exports it like so:

    @Global()
    @Module({
      imports: [HttpModule.register(httpModuleOptions)],
      exports: [HttpModule],
    })
    export class GlobalHttpModule {}
    

    Now import it in the AppModule and you can use HttpService anywhere