웹사이트 검색

타입스크립트 - 네스트JS에서 ES 모듈 해석


저는 서비스와 상수가 있는 두 개의 NestJS 모듈, AModule과 BModule을 가지고 있습니다.

AModule -> AService에서 저는 import { ASchemaKy } from '../const';처럼 ASchemaKey를 가져와서 생성자에서 사용합니다

import { ASchemaKey } from '../const';

export class AService {
 constructor(@InjectModel(ASchemaKey) private model) {}
}

BModule -> BService에서 생성자 안에 AService를 가져옵니다

export class BService {
 constructor(private aService: AService) {}
}

그리고 다음과 같은 오류가 발생했습니다 Nest가 UserService(?)의 종속성을 해결할 수 없습니다. 인덱스 [0]의 undefinedModel 인수가 AModule 컨텍스트에서 사용 가능한지 확인하세요.

디버깅을 통해 BService 파일이 AService 파일보다 먼저 실행되고, AService가 의존하는 ASchemaKey와 같은 모든 import가 정의되지 않았다는 것을 알아냈습니다.

AModule에서 가져올 수 있는 모든 것에 대해 고려됩니다. 예를 들어 import { ASchemaKey } from '@aModule/const'와 같이 가져오면 파일 실행 순서 때문에 ASchemaKey는 정의되지 않을 것입니다.

어떻게든 고칠 수 있을까요?

내 tsconfig

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "es2015",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true,
    "moduleResolution": "node",
    "importHelpers": true,
    
    "paths": {
      "@user/*": ["src/user/*"],
      "@auth/*": ["src/auth/*"],
      "@payment/*": ["src/payment/*"],
      "@config/*": ["src/config/*"],
      "@settings/*": ["src/settings/*"],
      "@common/*": ["src/common/*"]
    },
  }
}