title: NestJS - How to use .env variables in main app module file for database connection
body: <p>I am working on my first NestJS application, which was working fine with hardcoded database connecting string in <code>app.module.ts</code>.</p>
  <p>But then as per our requirements, I had to pick the database config values from environment files. For that, I followed the configuration documentation on the nestjs documentation website - <a href="https://docs.nestjs.com/techniques/configuration" rel="noreferrer">https://docs.nestjs.com/techniques/configuration</a></p>
  <p>But the issue is that I need to use the .env variables inside the same file for database connection, which is failing.</p>
  <p>Here is my original code that was working fine:</p>
  <pre><code>@Module({
    imports: [
      MongooseModule.forRoot(`mongodb+srv://myusername:mypassword@myhost.net?retryWrites=true&amp;w=majority&amp;db=dbname`, { useNewUrlParser: true, dbName: 'dbname' }),
      ProductModule,
      CategoryModule,
    ],
    controllers: [
      AppController,
      HealthCheckController,
    ],
    providers: [AppService, CustomLogger],
  })
  </code></pre>
  <p>Now, I wanted to pick those DB values from .env files which are like <code>local.env</code>, <code>dev.env</code> etc. depending on the environment. Now, my this code is not working:</p>
  <pre><code>@Module({
    imports: [
      ConfigModule.forRoot({ envFilePath: `${process.env.NODE_ENV}.env` }),
      MongooseModule.forRoot(`mongodb+srv://${ConfigModule.get('DB_USER')}:${ConfigModule.get('DB_PASS')}@myhost.net?retryWrites=true&amp;w=majority&amp;db=dbname`, { useNewUrlParser: true, dbName: 'dbname' }),
      ProductModule,
      CategoryModule,
    ],
    controllers: [
      AppController,
      HealthCheckController,
    ],
    providers: [AppService, CustomLogger],
  })
  </code></pre>