Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

667
Views
How to pass constructor arguments, to a NestJS provider?

I have some Service, which requires a config object literal in its constructor, something like this:

@Injectable()
export class BatteriesService { 

    constructor(private config: Config) { }//The provider needs a config object 

}

If I simply add this class in the providers array of the module, I obviously get an error, being that a constructor argument is missing.

So, instead of just referencing the BatteriesService class, I need to somehow create an instance. I tried this:

@Module({
  controllers: [BatteriesController],
  providers: [{
    useFactory: ()=>{
     return new BatteriesService({'someProp': 'someValue'})
    },
    provide:'BatteriesService'
  }]
})

And this:

@Module({
  controllers: [BatteriesController],
  providers: [{
    useValue:new BatteriesService({'someProp': 'someValue'}),
    provide:'BatteriesService'
  }]
})

In both cases I get the following error:

Error: Nest can't resolve dependencies of the BatteriesController (?). Please make sure that the argument BatteriesService at index [0] is available in the BatteriesModule context.

How can this done, without "resorting" to bypassing the DI system, or creating another "inner" provider(config)?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

When you need to have DI in service that has to be defined in the module.

In your case

@Injectable()
export class BatteriesService { 
  constructor(private config: ConfigService) { }
}

@Module({
  imports: [ConfigModule.forRoot({})], // Configure it as needed
  providers: [BatteriesService]
})

Your mistake is that you don't actually import ConfigModule while your service is dependent on it.

If you wish to use useFactory method then it would look like

@Module({
  providers: [{
    useFactory: (config: ConfigService) => {
      return new BatteriesService(config);
    },
    provide: BatteriesService,
    inject: [ConfigService]
  }]
})

I assumed your Config is actually Nest ConfigModule.

But if it's some custom Module you still need to import it as in the above examples.

If you want to pass an object literal as a config try this

interface MyConfigType = {
  something: string;
}
@Injectable()
export class BatteriesService { 
  constructor(@Inject('CONFIG') private config: MyConfigType) { }
}

@Module({
  providers: [{
    provide: 'CONFIG',
    useValue: {
      something: 'my-value'
    }
  }]
})
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!