首页 > 解决方案 > 每当我导入我的装饰器时,我都会收到 MODULE_NOT_FOUND

问题描述

我的项目是使用 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.jstypescripttypescript-decorator

解决方案


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


推荐阅读