Search code examples
typescriptnestjstypeorm

Nest can't resolve dependencies of the TypeOrmModuleOptions (?)


I have a typeorm.config.ts file and an app.module.ts file.

typeorm.config.ts

import { TypeOrmModuleOptions } from "@nestjs/typeorm";

export const typeORMConfig: TypeOrmModuleOptions = {
  type: "mysql",
  database: "abc",
  host: "abc",
  port: 1234,
  username: "root",
  password: "1234",
  logging: true,
  entities: ["dist/**/**.entity{.ts,.js}"],
  synchronize: true,
};

app.module.ts

import { Module } from "@nestjs/common";
import { TypeOrmModule } from "@nestjs/typeorm";
import { AuthModule } from "./auth/auth.module";
import { PostModule } from "./post/post.module";
import { typeORMConfig } from "./configs/typeorm.config";

@Module({
  imports: [
    TypeOrmModule.forRootAsync(typeORMConfig),
    AuthModule,
    PostModule,
  ],
})
export class AppModule {}

When I run npm run start, the following error appears.

Nest can't resolve dependencies of the TypeOrmModuleOptions (?). Please make sure that the argument dependency at index [0] is available in the TypeOrmCoreModule context.
Potential solutions:
- If dependency is a provider, is it part of the current TypeOrmCoreModule?
- If dependency is exported from a separate @Module, is that module imported within TypeOrmCoreModule?
  @Module({
    imports: [ /* the Module containing dependency */ ]
  })

I couldn't find the cause of that error. what am i doing wrong? I need help.


Solution

  • Looks like there is some problem with the way you are initializing TypeOrmModule

    There are three options forRoot, forFeature and forRootAsync

    Looking at the example you have provided, you want to load the configuration asynchronously with forRootAsync which expects TypeOrmModuleAsyncOptions as an argument

    In that case, your TypeOrmModule binding should look like following

    TypeOrmModule.forRootAsync({
          imports: [ConfigModule],
          useFactory: DatabaseConfiguration,
          inject: [ConfigService],
       })
    

    Where DatabaseConfiguration should be an async function that returns the following function

    export default (configService: ConfigService): TypeOrmModuleOptions => {
      const options: TypeOrmModuleOptions = {
        type: 'mysql',
        host: configService.get('database.host'),
        port: +configService.get<number>('database.port'),
        username: configService.get('database.user'),
        password: configService.get('database.password'),
        database: configService.get('database.name'),
        entities: [/** entities here **/],
        synchronize: false,
      };
      return options;
    };
    

    And to load database credentials and other variables from the environment variables or other secret managers you should use ConfigService from ConfigModule

    If you want to load database credentials and other details synchronously then you should use forRoot which expects TypeOrmModuleOptions as an argument as follows

    TypeOrmModule.forRoot({ \** JSON here **\})