문제

순환 의존성(circular dependency)으로 인해 모듈이 import 되지 않습니다.

AppModule → AuthModule → UserModule → AuthModule 구조가 되어 순환 의존성이 발생합니다.

Untitled

@Module({
  imports: [TypeOrmModule.forFeature([User]), AuthModule],
  controllers: [UserController],
  providers: [ UserService, UserRepository, ThemeRepository, GenreRepository ],
  exports: [UserService],
})
export class UserModule {}

해결 과정

문제 해결 과정을 기록합니다.

🚫 Providers에 AuthService 추가하기

AuthService 에서는 HttpServiceJwtService 두 개의 추가적인 의존성을 지니고 있지만, 이를 AuthModule 에서만 주입해주고 있기에 이를 사용하기 위해서 AuthService 만 주입하고 있기에 여전히 에러가 발생했습니다.

@Module({
  imports: [TypeOrmModule.forFeature([User])],
  controllers: [UserController],
  providers: [
    UserService,
    UserRepository,
    ThemeRepository,
    GenreRepository,
    AuthService,
  ],
  exports: [UserService],
})
export class UserModule {}

Untitled

🚫 Providers에 AuthServiceHttpService, JwtService 를 주입하기

@Module({
  imports: [TypeOrmModule.forFeature([User])],
  controllers: [UserController],
  providers: [
    UserService,
    UserRepository,
    ThemeRepository,
    GenreRepository,
    AuthService,
    JwtService,
    HttpService,
  ],
  exports: [UserService],
})
export class UserModule {}

Untitled

✅🚫 Providers에 AuthService 와 Imports에 HttpModule, Jwt Module 를 주입하기

해당 방식을 통해 AuthService에서 다시 HttpModule과 JwtModule을 주입받아 사용할 수 있도록 합니다.

이런 방식을 이용하면 동일하게 이용할 수 있습니다.

@Module({
  imports: [TypeOrmModule.forFeature([User]), HttpModule, JwtModule],
  controllers: [UserController],
  providers: [UserService, UserRepository, ThemeRepository, GenreRepository, AuthService],
  exports: [UserService],
})
export class UserModule {}