Nest无法解析TMPController的依赖,求助解决方案
解决Nest无法解析TMPController依赖的问题
问题根源
你的tmp.controller.ts中对TMPService使用了类型仅导入(type-only import):
import type { TMPService } from "./tmp.service";
TypeScript编译后会完全移除类型相关代码,导致Nest运行时无法识别TMPService的类引用,进而无法找到对应的依赖提供者。
修复方案
将控制器中的导入语句修改为正常的类导入,移除type关键字:
// tmp.controller.ts import { Controller, Get } from "@nestjs/common"; import type { APICompanyMembers, APICompanyNews } from "@truckersmp_official/api-types/v2"; // 去掉type,改为正常导入 import { TMPService } from "./tmp.service"; @Controller("tmp") export class TMPController { constructor(private readonly tmpService: TMPService) { }; @Get("news") async findNews(): Promise<{ response: APICompanyNews }> { return await this.tmpService.getNews(); }; };
验证其他配置
你的TMPModule已经正确注册了TMPController和TMPService,TMPService也添加了@Injectable()装饰器,这些配置都是正确的,只需修正导入方式即可解决依赖解析错误。
内容的提问来源于stack exchange,提问作者djoh
相关产品推荐
相关产品推荐

