Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NestJS HTTP Module needs to be imported in every feature module

Tags:

nestjs

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.

like image 974
Rajib Dey Avatar asked Oct 31 '25 18:10

Rajib Dey


2 Answers

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

like image 101
Jay McDoniel Avatar answered Nov 03 '25 09:11

Jay McDoniel


If you don't want to create a wrapper module, you can also:

@Module({
  imports: [
    {
      ...HttpModule.register(httpModuleOptions),
      global: true
    }
  ],
})
export class AppModule {}
like image 38
German Velazquez Avatar answered Nov 03 '25 10:11

German Velazquez



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!