title: Unable to inject a class-based provider in a function expression in nestjs
body: <p>I have created the following project structure using NestJS</p>
  <pre><code>.
  ├── app.controller.ts
  ├── app.module.ts
  ├── app.service.ts
  ├── config
  │   ├── config.controller.ts
  │   ├── config.module.ts
  │   └── config.service.ts
  ├── handlers
  │   ├── handler1.ts
  │   ├── handler2.ts
  │   └── handlers.module.ts
  ├── main.ts
  └── producer
      └── producer.ts
  </code></pre>
  <p>Both the handler files wiz. <code>handler1.ts</code> &amp; <code>handler2.ts</code> look something like this:</p>
  <pre><code>export const handler = async (args) {
  ...
  ...
  }
  </code></pre>
  <p>I've <code>ConfigModule</code> in which I've registered <code>ConfigService</code> as a provider. What I want to do is I want to somehow use <code>ConfigService</code> in <code>handler1.ts</code> &amp; <code>handler2.ts</code>. Now mind you, these handlers are not classes, but just normal function expressions. I know if they were classes, I could have injected <code>ConfigService</code> in the handler's constructor using Dependency Injection. But, unfortunately, declaring these handlers as classes is no more an option now. The reason behind that is that these handler files are being consumed by <code>producer.ts</code> and <code>producer.ts</code> reads the entire file and expects an exported function.</p>
  <p>I went through the NestJS documentation and found about <a href="https://docs.nestjs.com/providers#property-based-injection" rel="nofollow noreferrer">property-based injection</a>, <a href="https://docs.nestjs.com/fundamentals/module-ref" rel="nofollow noreferrer">module-ref</a>, but they were of no help. I also found this <a href="https://stackoverflow.com/questions/58666705/injecting-a-service-into-controller-function-in-nestjs">link</a> which I think is very close to my problem, But after going through the comments I found out this is not possible.</p>
  <p>I don't have much liberty in changing the existing code but I'd still like to know what options do I have here. A solution that would require fewer changes and solve my problem. Thanks!</p>
  <p><strong>Note</strong>: File <code>handlers.module.ts</code> is serving no purpose in this scenario.</p>