question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use the config service to configure other modules?

See original GitHub issue

Great library, but I’m having issues using the ConfigService to configure other modules. Can you provide some example?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:11

github_iconTop GitHub Comments

30reactions
EthanSchoencommented, Jun 30, 2021

For anyone here from google like me this is what I needed:

Documentation: https://docs.nestjs.com/techniques/mongodb#async-configuration

@Module({
  imports: [
    MongooseModule.forRootAsync({
      imports: [ConfigModule],
      useFactory: async (configService: ConfigService) => ({
        uri: configService.get<string>('MONGODB_URI'),
      }),
      inject: [ConfigService],
    }),
  ],
})
export class AppModule {}
13reactions
gremocommented, Aug 1, 2018

… that requires the configservice to be passed in rather than loaded within the module itself therefore loading the config before all modules

I was thinking about the a few days ago. Ugly solution IMHO.

I hope that the issue on nestjs will be solved. In the end, it’s all about modules depending on services (@marcus-sa solution):

@Module({
  imports: [{
    useModule: (config: ConfigService) => MongooseModule.forRoot(config.mongoUri),
    inject: [ConfigService]
  }],
  providers: [ConfigService],
})
export class AppModule { }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Best practice to use config service in NestJS Module
The useFactory method returns the configuration object. TypeOrmModule.forRootAsync({ imports:[ConfigModule], useFactory: async (configService ...
Read more >
Configuration | NestJS - A progressive Node.js framework
A good approach for using this technique in Nest is to create a ConfigModule that exposes a ConfigService which loads the appropriate .env...
Read more >
Using the NestJS Config Module to Manage Environment ...
To access the configuration values start by importing ConfigService from @nestjs/config. Inject it into the class's constructor by declaring a ...
Read more >
Externalizing application Configuration | by Hantsy | The Startup
First of all, install @nestjs/config package. ... Simply, import ConfigModule in the top-level AppModule . ... It will register a ConfigService for ...
Read more >
NestJS Config Module: Using environment variables - Tom Ray
Install dependencies · Add the Config Module configuration · You can now use process.env · Using custom configuration files · Validating environment ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found