【问题标题】:I'm getting MODULE_NOT_FOUND whenever i import my decorator每当我导入我的装饰器时,我都会收到 MODULE_NOT_FOUND
【发布时间】:2021-11-28 10:23:12
【问题描述】:

我的项目是使用 Express 的后端。我试图使用装饰器创建一个自动招摇。然后我创建了一个名为decorators.ts 的文件和一个名为dynamic-loader.ts 的文件。 我的动态加载器只是在控制器文件夹内的每个文件中使用import。在我的decorators.ts 中,我有许多装饰器,如下所示:

    ...

    export function ApiController(route: string, name?: string) {
        return function (
            target: any
        ) {
            target.prototype.route = route;
            target.prototype.tag = name ?? (<string>target.name).replace(/Controller/g, '');
        };
    }
    
    export function AllowAnonymous(
        target: any,
        propertyKey: string,
        descriptor: PropertyDescriptor
    ) {
        descriptor.value.anonymous = true;
    }

    ...

问题是,每当我将任何装饰器导入控制器时,都会出现此错误:

    Error: Cannot find module 'src/lib/decorators'

我的 tsconfig.json 看起来像这样:

{
  "compilerOptions": {
    "baseUrl": "./",
    "lib": [
      "es5",
      "es6"
    ],
    "target": "es2020",
    "module": "commonjs",
    "outDir": "dist",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "strict": true,
    "strictNullChecks": false,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "sourceMap": true,
    "allowJs": true
  },
  "include": [ "src/**/*" ],
  "exclude": [ "node_modules" ]
}

【问题讨论】:

    标签: node.js typescript typescript-decorator


    【解决方案1】:

    我再次使用 yarn 安装了每个依赖项,现在它可以工作了。

    【讨论】:

      猜你喜欢
      • 2021-10-18
      • 2015-07-20
      • 1970-01-01
      • 2022-12-26
      • 1970-01-01
      • 2013-03-22
      • 1970-01-01
      • 1970-01-01
      • 2022-01-26
      相关资源
      最近更新 更多