How to use config service within app.module.ts
See original GitHub issueI have the following folder structure (as given in the getting started section of the readme)
/src
├── app.module.ts
├── config
│ ├── app.ts
...
In my app.module.ts, I have
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ConfigModule, ConfigService } from 'nestjs-config';
@Module({
imports: [ConfigModule.load()],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {
constructor(private readonly config: ConfigService) {
this.config = config;
console.log(config); // ConfigService { helpers: {} }
AppModule.host = this.config.get('app.url');
console.log( 'App URL is', this.config.get('app.url') ); // App URL is undefined
}
...
}
What could I be doing wrong?
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (1 by maintainers)
Top Results From Across the Web
Is there a way to use configService in App.Module.ts?
You have to use a dynamic import (see Async configuration). With it, you can inject dependencies and use them for the initialization:
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 >NestJS Config Module: Using environment variables - Tom Ray
Learn how to use environment variables (and other configuration values) in your NestJS projects with the Config Module.
Read more >Config File Setup In NestJs - Abiral Sthapit - Medium
Create a .env file in the src of the application and lets assume we have following environment variables. APP_ENV=dev. APP_NAME=MY APP NAME
Read more >Using the NestJS Config Module to Manage Environment ...
In your project's root module (app.modue. · Add ConfigModule to your imports array and call the forRoot method on it. · Pass a...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
It will also be good to show how to make use of this in the bootstrap/main.ts itself. Given that access to the port is needed for example.
Ideally, config service should be one of the first things loaded in an app before even the server is started, once loaded it should be available globally always.
Also, it would be nice that if it can easily (read seamlessly) be injected as middleware so it reloads at runtime and only if files changed for perf reasons.
See : Golang Env Config for some ideas. I have used this go module previously.
~Any news on this one? Currently my only workaround is defining the service in the constructor and access the env variables by
process.env
directly. This defies to whole purpose of the ConfigService than only exposing the environment variables…~@bashleigh Please update the readme to remove the example of the empty ConfigModule.load(). For this to work its best to always define the path.