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:
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,
) {}
}
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.