• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

NestJS 의존성 주입 문제

24.03.17 13:04 작성 조회수 89

0

 [Nest] 16188  - 2024. 03. 17. 오후 12:19:06   ERROR [ExceptionHandler] Nest can't resolve dependencies of the UsersService (?). Please make sure that the argument "UserRepository" at index [0] is available in the AuthModule context.

Potential solutions:
- Is AuthModule a valid NestJS module?
- If "UserRepository" is a provider, is it part of the current AuthModule?
- If "UserRepository" is exported from a separate @Module, is that module imported within AuthModule?
  @Module({
    imports: [ /* the Module containing "UserRepository" */ ]
  })

Error: Nest can't resolve dependencies of the UsersService (?). Please make sure that the argument "UserRepository" at index [0] is available in the AuthModule context.

Potential solutions:
- Is AuthModule a valid NestJS module?
- If "UserRepository" is a provider, is it part of the current AuthModule?
- If "UserRepository" is exported from a separate @Module, is that module imported within AuthModule?
  @Module({
    imports: [ /* the Module containing "UserRepository" */ ]
  })

    at Injector.lookupComponentInParentModules (D:\BackEnd\class\section11\11-02-login-authentication\node_modules\@nestjs\core\injector\injector.js:254:19)
    at Injector.resolveComponentInstance (D:\BackEnd\class\section11\11-02-login-authentication\node_modules\@nestjs\core\injector\injector.js:207:33)
    at resolveParam (D:\BackEnd\class\section11\11-02-login-authentication\node_modules\@nestjs\core\injector\injector.js:128:38)
    at async Promise.all (index 0)
    at Injector.resolveConstructorParams (D:\BackEnd\class\section11\11-02-login-authentication\node_modules\@nestjs\core\injector\injector.js:143:27)
    at Injector.loadInstance (D:\BackEnd\class\section11\11-02-login-authentication\node_modules\@nestjs\core\injector\injector.js:70:13)
    at Injector.loadProvider (D:\BackEnd\class\section11\11-02-login-authentication\node_modules\@nestjs\core\injector\injector.js:97:9)
    at D:\BackEn

yarn start:dev 실행 시, 위와 같은 오류가 발생하여, users.module.ts 파일의 exports 부분을 아래와 같이 수정하였습니다.

exports: [
        TypeOrmModule,
        //   원래는 TypeOrmModule이 아닌 => UsersService(강의 기준)
    ],

이렇게 수정하니, 문제는 해결되었는데, 이렇게 수정해도 괜찮은지, 이유가 무엇인지 잘 모르겠어 여쭤봅니다.

답변 1

답변을 작성해보세요.

0

인프런 AI 인턴님의 프로필

인프런 AI 인턴

2024.03.19

안녕하세요, 인프런 AI 인턴입니다.
hun님, NestJS에서 발생한 의존성 주입 문제에 대해서 질문해주셨군요.

에러 메시지에 따르면 UsersService가 의존하는 UserRepositoryAuthModule 컨텍스트에서 찾을 수 없다고 합니다. 이 경우 가장 먼저 확인해야 할 것은 UserRepository가 제대로 프로바이더로 등록되어 있는지 그리고 AuthModule에 올바르게 임포트되었는지입니다.

exportsTypeOrmModule를 추가하는 대신 UsersService를 추가한 것은 TypeOrmModuleUserRepository를 내보내는 역할을 하고 있기 때문에 해결된 것으로 보입니다. 만약 TypeOrmModule 설정에서 UserRepository를 포함하고 있다면, TypeOrmModuleexports에 추가하는 것이 맞습니다. 이렇게 하면 다른 모듈에서 TypeOrmModule를 임포트할 때 UserRepository도 같이 임포트되어 사용할 수 있게 됩니다.

UsersService를 직접 exports에 추가하는 것보다는 해당 서비스가 사용하는 리포지토리나 모델을 내보내는 모듈을 추가하는 것이 올바른 방식입니다. 다른 모듈에서는 해당 서비스 대신 필요한 리포지토리를 주입받아 사용할 수 있으니까요.

종합해 보면, UserRepository를 포함하는 TypeOrmModuleexports에 추가하는 것이 적절한 해결 방법입니다. 하지만, 왜 이런 변화가 문제를 해결하는지 이해하는 것이 중요하답니다. TypeOrmModule 내에서 UserRepository가 어떻게 제공되고 있는지 코드를 살펴보시면 더 명확한 이해가 가능할 것입니다.