首页 > 解决方案 > Nestjs中的es模块解析

问题描述

我有两个带有服务和常量的 Nestjs 模块 AModule 和 BModule。

在 AModule -> AService 中,我像导入 ASchemaKeyimport { ASchemaKy } from '../const'; 并在构造函数中使用它

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

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

在 BModule -> BService 我在构造函数中导入 AService

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

并得到了错误Nest can't resolve dependencies of the UserService (?). Please make sure that the argument undefinedModel at index [0] is available in the AModule context

通过调试,我发现 BService 文件比 AService 文件执行得更早,并且 AService 依赖的所有导入(如 ASchemaKey)都是未定义的

它适用于所有可能从 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/*"]
    },
  }
}

标签: typescriptnestjses6-modules

解决方案


推荐阅读