title: How to create a Service that acts as a singleton with NestJS
body: <p>I am trying to create a Module that provdies a singleton service. Imagine a <code>QueueService</code>, simplest implementation would be a singleton service.</p>
  
  <p>Reproducible repository: <a href="https://github.com/colthreepv/nestjs-singletons" rel="noreferrer">https://github.com/colthreepv/nestjs-singletons</a></p>
  
  <p><strong>WALL OF CODE</strong></p>
  
  <p>app.module.ts:</p>
  
  <pre class="lang-js prettyprint-override"><code>@Module({ imports: [FirstConsumerModule, SecondConsumerModule] })
  export class AppModule {}
  </code></pre>
  
  <p>firstconsumer.module.ts <strong>and</strong> secondconsumer.module.ts (they are identical):</p>
  
  <pre class="lang-js prettyprint-override"><code>@Injectable()
  class FirstConsumer {
    constructor(private readonly dependency: DependencyService) {}
  }
  
  @Module({
    imports: [DependencyServiceModule],
    providers: [DependencyService, FirstConsumer]
  })
  export class FirstConsumerModule {
    constructor(private readonly first: FirstConsumer) {}
  }
  </code></pre>
  
  <p>dependency.module.ts:</p>
  
  <pre class="lang-js prettyprint-override"><code>@Injectable()
  export class DependencyService {
    constructor() { console.log("Instance created") }
  }
  
  @Module({ providers: [DependencyService], exports: [DependencyService] })
  export class DependencyServiceModule {}
  </code></pre>
  
  <p><strong>CODE DONE</strong></p>
  
  <p>What I would like to obtain is having console.log <code>Instance created</code> just be posted once.<br>
  At the moment:</p>
  
  <pre><code>[NestFactory] Starting Nest application...
  Instance created
  Instance created
  Instance created
  [InstanceLoader] AppModule dependencies initialized +16ms
  [InstanceLoader] DependencyServiceModule dependencies initialized +1ms
  [InstanceLoader] FirstConsumerModule dependencies initialized +1ms
  [InstanceLoader] SecondConsumerModule dependencies initialized +1ms
  [NestApplication] Nest application successfully started +8ms
  </code></pre>