You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 13:30:59