node.js - 每当我导入我的装饰器时,我都会收到 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" ]
}
解决方案
我使用 yarn 再次安装了每个依赖项,现在它可以工作了。
推荐阅读
- c# - SQL 如何替换一个值
- python - 使用 python 和请求,如何进行正确的 POST 调用以及在哪里找到标头?
- android - FirebaseAppDistribution:appDistributionUpload gradle 命令中缺少应用程序 ID
- mysql - 跨列和行计算值的实例
- javascript - 使用 redux 钩子时使用 redux 操作的最佳选择是什么?
- sql - Impala 2.11:AnalysisException:选择列表中不支持子查询
- swift - 在 SwiftUI 中获取当前的经纬度
- c++ - 是否可以禁止一个类被动态转换为?
- git - Git - 将发布分支合并到主分支
- tensorflow - 任何想法如何解决激活函数的问题?