Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

904
Visualizações
How to inject a nestjs service into another service when both belong to the same module?

I have the following scenario in NestJS:

// user.service.ts
@Injectable()
export class UserService {
  constructor(
    private readonly userRepository: UserRepository,
    private readonly userProfileService: UserProfileService,
  ) {}
}


// user-profile.service.ts
@Injectable()
export class UserProfileService {
  constructor(
    private readonly userProfileRepository: UserProfileRepository,
  ) {}
}


// user.module.ts
@Module({
imports: [DataRepositoryModule], // Required for the repository dependencies
  providers: [UserService, UserProfileService],
  exports: [UserService, UserProfileService],
})
export class UserModule {}

However, when I try to use the UserService inside a controller from another module, I get the following error:

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

Controller code:

@Controller()
export class UserController {
  constructor(private readonly userService: UserService) {}
}

Controller module:

@Module({
  imports: [],
  providers: [],
  controllers: [UserController],
})
export class UserManagementModule {}

Main app.module.ts:

@Module({
  imports: [
    UserManagementModule,
    UserModule,
    DataRepositoryModule.forRoot(),
  ],
  controllers: [],
  providers: [],
})
export class AppModule {}

I'm confused because I'm doing exactly what the error suggests, adding both services inside the providers array (UserModule). What could I be missing?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

From your error, it looks like there's a circular dependency between your file imports. Check to make sure that you don't have a circular import chain going on (i.e. ServiceA imports ServiceB imports ServiceA or ServiceA imports ModuleA imports ServiceB imports ServiceA). This can especially become common common when using barrel files inside the same module.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda