我有一个已创建的类,并将其放在src文件夹中的自定义文件夹中。它不是nestjs模块,也不是提供程序,只是我可以实例化和使用的帮助程序类。但是,当我运行该应用程序时,出现以下错误:
npm run start:dev
> rent-go@1.0.0 start:dev /Users/arsenegandote/Applications/js/rent-go
> nodemon
[nodemon] 1.18.9
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: /Users/arsenegandote/Applications/js/rent-go/src/**/*
[nodemon] starting `ts-node -r tsconfig-paths/register src/main.ts`
Error: Cannot find module './json.result.js'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:603:15)
at Function.Module._resolveFilename (/Users/arsenegandote/Applications/js/rent-go/node_modules/tsconfig-paths/lib/register.js:75:40)
at Function.Module._load (internal/modules/cjs/loader.js:529:25)
at Module.require (internal/modules/cjs/loader.js:658:17)
at require (internal/modules/cjs/helpers.js:22:18)
at Object.<anonymous> (/Users/arsenegandote/Applications/js/rent-go/src/lib/api.controller.ts:1:1)
at Module._compile (internal/modules/cjs/loader.js:722:30)
at Module.m._compile (/Users/arsenegandote/Applications/js/rent-go/node_modules/ts-node/src/index.ts:439:23)
at Module._extensions..js (internal/modules/cjs/loader.js:733:10)
at Object.require.extensions.(anonymous function) [as .ts] (/Users/arsenegandote/Applications/js/rent-go/node_modules/ts-node/src/index.ts:442:12)
[nodemon] app crashed - waiting for file changes before starting...](url)
这是我要在控制器中实例化的通用类:
export class JsonResult<T> {
readonly status: string;
readonly data: T;
constructor(status: string, data: T) {
this.status = status;
this.data = data;
}
}
我的控制器实现了这个抽象类:
export abstract class ApiController {
failed(): void {
throw new HttpException(
{
status: ApiStatus.FAILED,
data: {},
},
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
notFound(): void {
throw new HttpException(
{
status: HttpStatus.NOT_FOUND,
data: [],
},
HttpStatus.NOT_FOUND,
);
}
badRequest(message: string): void {
throw new HttpException(
{
status: HttpStatus.BAD_REQUEST,
data: {
message,
},
},
HttpStatus.BAD_REQUEST,
);
}
success<T>(data: T): JsonResult<T> {
return new JsonResult<T>(ApiStatus.SUCCESS, data);
}
}
答案 0 :(得分:0)
Error: Cannot find module './json.result.js'
该错误表示找不到您的文件。确保导入路径正确。