NestJS自定义Schematic报错:无法解析'rest'的factory
解决Schematic "rest"无法解析工厂的问题
核心问题是配置与代码的函数名不匹配:
你的collection.json中rest schematic指定的工厂函数是applicationRest,但在对应的./rest-generator/index.ts文件里,实际导出的函数名为application,导致系统找不到对应的工厂函数。
两种修复方案任选其一:
方案1:修改collection.json的工厂函数名
将rest配置里的factory后缀从#applicationRest改为#application,匹配现有代码的导出函数:
"rest": { "description": "Nodejs Rest generator project scaffold", "factory": "./rest-generator/index#application", "schema": "./rest-generator/rest-schema.json" }
方案2:修改TypeScript文件的导出函数名
如果需要保留collection.json中的applicationRest配置,就把TS文件里的application函数重命名为applicationRest:
export function applicationRest(options: any) { // eslint-disable-next-line @typescript-eslint/no-unused-vars return (_tree: Tree, _context: SchematicContext) => { return chain([ externalSchematic('@nestjs/schematics', 'application', options), generate(options), ]); }; } export function generate(_options: any): Rule { return (tree: Tree, context: SchematicContext) => { return chain([ addDependencies(tree, _options), renderTemplates(_options), modifyGeneratedFiles(_options), ])(tree, context); }; }
额外检查项
- 确认
./rest-generator/index.ts的文件路径正确,和collection.json的相对位置没有偏差 - 检查函数名拼写,确保大小写、单词完全一致(比如不要把
applicationRest写成ApplicationRest)
内容的提问来源于stack exchange,提问作者Krish909
相关产品推荐
相关产品推荐

