6

I am trying to use ConfigService in my users.module.ts but I am getting an

Error: Nest can't resolve dependencies of the UsersService (UserRepository, HttpService, ?). Please make sure that the argument ConfigService at index [2] is available in the UsersModule context.

Potential solutions:

  • If ConfigService is a provider, is it part of the current UsersModule?
  • If ConfigService is exported from a separate @Module, is that module imported within UsersModule?

I have imported the ConfigModule in my UsersModule but still its not working :(

app.module.ts

@Module({
  imports: [
    ConfigModule.forRoot({
      expandVariables: true,
    }),
    TypeOrmModule.forRoot(),
    UsersModule,
    AuthModule,
  ],
  controllers: [AppController],
  providers: [AppService],
})

export class AppModule {}

users.module.ts

import { ConfigModule } from '@nestjs/config';

@Module({
  imports: [ConfigModule, HttpModule, TypeOrmModule.forFeature([User])],
  controllers: [UsersController],
  providers: [UsersService],
  exports: [UsersService],
})

export class UsersModule {}

users.service.ts

export class UsersService {

 constructor(
    @InjectRepository(User)
    private readonly userRepository: Repository<User>,
    private readonly httpService: HttpService,
    private readonly configService: ConfigService,
  ) {}

}
Muhammad Zeeshan
  • 4,608
  • 4
  • 15
  • 41
Istiyak Tailor
  • 1,570
  • 3
  • 14
  • 29

2 Answers2

9

You would still have to declare ConfigService in the providers of users.module.ts. Refer example below.

app.module.ts

@Module({
  imports: [
    ConfigModule.forRoot({
      expandVariables: true,
    }),
    TypeOrmModule.forRoot(),
    UsersModule,
    AuthModule,
  ],
  controllers: [AppController],
  providers: [AppService],
})

export class AppModule {}

users.module.ts

import { ConfigModule } from '@nestjs/config';

@Module({
  imports: [ConfigModule, HttpModule, TypeOrmModule.forFeature([UserRepository])],
  controllers: [UsersController],
  providers: [UsersService, ConfigService],
  exports: [UsersService],
})

export class UsersModule {}

Hope this helps.

Thanks.

Tek Loon
  • 206
  • 1
  • 10
  • Setting it to Global does work but what I wanted is to explicitly import it wherever required. You answer is indeed correct but the reason why I posted this question is because I wanted to know what it is not working even though everything is correct – Istiyak Tailor Feb 13 '20 at 14:40
  • Hi @IstiyakTailor, In order to make it work, aside from import it. you still have to declare it as providers in that module itself. Refer to the example I edit above. – Tek Loon Feb 14 '20 at 00:24
  • 1
    It was a [bug](https://github.com/nestjs/config/issues/82). They fixed it. – Gleb Varenov Jun 22 '20 at 06:25
  • This answer could be improved by including only code that has changed, and some text calling out what changed. As it stands it may be hard for users to see what you did that answers op's question. – Madbreaks Apr 14 '22 at 16:33
1

You got things kinda crossed (I went through the same pain starting out too). Best practice would be to create a custom repository to handle the database logic.

First declare a UserRepository:

@EntityRepository(User)
export class UserRepository extends Repository<User> {

    // add your custom db related method here later..

}

Then in your AppModule you need to declare your entities like so:

@Module({

    imports: [

        TypeOrmModule.forRoot({

            type: 'mysql',
            host: process.env.DB_HOSTNAME || 'localhost',
            port: Number.parseInt(process.env.DB_PORT) || 3306,
            username: process.env.DB_USERNAME || 'root',
            password: process.env.DB_PASSWORD || 'mysql',
            database: process.env.DB_NAME || 'nestjs',
            synchronize: process.env.DB_SYNCHRONIZE === 'true' || true,
            keepConnectionAlive: true,
            entities: [

                User

            ]

        })

...

And then in your UsersModule declare your repository:

import { ConfigModule } from '@nestjs/config';

@Module({
  imports: [ConfigModule, HttpModule, TypeOrmModule.forFeature([UserRepository])],
  controllers: [UsersController],
  providers: [UsersService],
  exports: [UsersService],
})

export class UsersModule {}
yomateo
  • 2,078
  • 12
  • 17