每当我导入我的装饰器时,我都会得到 MODULE_NOT_FOUND
I'm getting MODULE_NOT_FOUND whenever i import my decorator
我的项目是使用 Express 的后端。我试图使用装饰器创建一个自动的 swagger。然后我创建了一个名为 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 再次安装了每个依赖项,现在它可以工作了。
我的项目是使用 Express 的后端。我试图使用装饰器创建一个自动的 swagger。然后我创建了一个名为 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 再次安装了每个依赖项,现在它可以工作了。