title: NestJs - TypeORM configuration works but not with ConfigService
body: <p>I would like to create a REST API with NestJs and TypeORM. In my <strong>app.module.ts</strong> I load the TypeORM module</p>
  
  <pre><code>@Module({
    imports: [
      TypeOrmModule.forRoot({
        type: 'postgres',
        host: 'localhost',
        port: 5432,
        username: 'postgres',
        password: 'postgres',
        database: 'api',
        entities: [`${__dirname}/**/*.entity.{ts,js}`],
        synchronize: true,
      }),
    ],
  })
  export class AppModule {}
  </code></pre>
  
  <p>and it's working fine for now. I would like to load the configuration from an external .env file so from the docs</p>
  
  <p><a href="https://docs.nestjs.com/techniques/database#async-configuration" rel="nofollow noreferrer">https://docs.nestjs.com/techniques/database#async-configuration</a></p>
  
  <p>and from here</p>
  
  <p><a href="https://stackoverflow.com/questions/52570212/nestjs-using-configservice-with-typeormmodule">NestJS Using ConfigService with TypeOrmModule</a></p>
  
  <p>I created a .env file in the root project directory with the following content</p>
  
  <pre><code>DATABASE_TYPE = postgres
  DATABASE_HOST = localhost
  DATABASE_PORT = 5432
  DATABASE_USERNAME = postgres
  DATABASE_PASSWORD = postgres
  DATABASE_NAME = api
  DATABASE_SYNCHRONIZE = true
  </code></pre>
  
  <p>Next I update my code to</p>
  
  <pre><code>@Module({
    imports: [
      ConfigModule.forRoot(),
      TypeOrmModule.forRootAsync({
        imports: [ConfigModule],
        useFactory: async (configService: ConfigService) =&gt; ({
          type: configService.get&lt;any&gt;('DATABASE_TYPE'),
          host: configService.get&lt;string&gt;('DATABASE_HOST'),
          port: configService.get&lt;number&gt;('DATABASE_PORT'),
          username: configService.get&lt;string&gt;('DATABASE_USERNAME'),
          password: configService.get&lt;string&gt;('DATABASE_PASSWORD'),
          database: configService.get&lt;string&gt;('DATABASE_NAME'),
          entities: [`${__dirname}/**/*.entity.{ts,js}`],
          synchronize: configService.get&lt;boolean&gt;('DATABASE_SYNCHRONIZE'),
        }),
        inject: [ConfigService],
      }),
    ],
  })
  export class AppModule {}
  </code></pre>
  
  <p>Unfortunately I get this error on startup</p>
  
  <pre><code>[Nest] 28257   - 01/06/2020, 7:19:20 AM   [ExceptionHandler] Nest can't resolve dependencies of the TypeOrmModuleOptions (?). Please make sure that the argument ConfigService at index [0] is available in the TypeOrmCoreModule context.
  
  Potential solutions:
  - If ConfigService is a provider, is it part of the current TypeOrmCoreModule?
  - If ConfigService is exported from a separate @Module, is that module imported within TypeOrmCoreModule?
    @Module({
      imports: [ /* the Module containing ConfigService */ ]
    })
   +1ms
  </code></pre>
  
  <p>When I log the configuration in my <strong>main.ts</strong> within the <strong>bootstrap</strong> function I get a correct configuration from the .env file.</p>
  
  <p>How can I fix the error?</p>