Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save temp3l/1c3dab2ca27ccb2bf947635860cb55de to your computer and use it in GitHub Desktop.
Save temp3l/1c3dab2ca27ccb2bf947635860cb55de to your computer and use it in GitHub Desktop.
nestjs singleton module instance dependency

The trick is:

  • do not import/export/provide the DataService from ANY module (including app.module)
  • just import the ConsumingService from app.module

DataService implements onModuleInit
    fetch countries, docTypes, etc
DataModule:
    providers: [DataService],
    exports: [DataService]

FirstConsumerService
    constructor(private readonly dependency: DataService) { }
FirstConsumerModule
    imports: [DataModule],
    providers: [FirstConsumerService]

AppModule
    imports: [FirstConsumerModule]

From: https://stackoverflow.com/questions/60192912/how-to-create-a-service-that-acts-as-a-singleton-with-nestjs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment