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 config service within app.module.ts

See original GitHub issue

I 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:closed
  • Created 5 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
charbel14commented, Jan 3, 2019

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.

0reactions
mrpharderwijkcommented, Jan 22, 2019

~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.

Read more comments on GitHub >

github_iconTop 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 >

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