5

I'm following this book: https://github.com/manfredsteyer/schematics-sample

When I execute my schematics

schematics .:my-comp

I get the following error:

An error occured:
Error: Schematic "my-comp" cannot resolve the factory.
    at NodeModulesEngineHost.createSchematicDescription (/home/.../.npm-global/lib/node_modules/@angular-devkit/schematics-cli/node_modules/@angular-devkit/schematics/tools/file-system-engine-host-base.js:174:19)
    at SchematicEngine.createSchematic (/home/.../.npm-global/lib/node_modules/@angular-devkit/schematics-cli/node_modules/@angular-devkit/schematics/src/engine/engine.js:219:38)
    at CollectionImpl.createSchematic (/home/.../.npm-global/lib/node_modules/@angular-devkit/schematics-cli/node_modules/@angular-devkit/schematics/src/engine/engine.js:69:29)
    at NodeWorkflow.execute (/home/.../.npm-global/lib/node_modules/@angular-devkit/schematics-cli/node_modules/@angular-devkit/schematics/src/workflow/base.js:99:38)
    at main (/home/.../.npm-global/lib/node_modules/@angular-devkit/schematics-cli/bin/schematics.js:202:24)
    at Object.<anonymous> (/home/.../.npm-global/lib/node_modules/@angular-devkit/schematics-cli/bin/schematics.js:293:5)
    at Module._compile (internal/modules/cjs/loader.js:701:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
    at Module.load (internal/modules/cjs/loader.js:600:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:539:12)

This is part of my factory:

    export default function myComp(options: IFlexComponentOptions): Rule {
  return (host: Tree, context: SchematicContext) => {
    console.log("options before sanatize", options);
    ...
    return rule(host, context);
  };
}

my collectyion.json

{
  "$schema": "../node_modules/@angular-devkit/schematics/collection-schema.json",
  "schematics": {
    "my-comp": {
      "description": "This schematics generate an Angular Component on the current module or in an specified one",
      "factory": "./my-comp/index#myComp"
    }
  }
}
Jorge C
  • 63
  • 7

2 Answers2

3

I had the same problem, from export default function myComp remove the default. And it should work. I follow the same book. Lots of incompatibilities.

titusfx
  • 1,896
  • 27
  • 36
  • 1
    It has to do with how the `collections.json` file references the factory - if you have the function exported as default, you do not need the `#helloWorld` in the factory reference. If you do keep the hash, that will be the name of the exported function, in this case, `helloWorld`, and if it was exported default that function name doesn't exist. So to reference the default export, do not include hash on the `collection.json` factory property, and with no default export, you need the hash to identify which exported function to reference. – LocalPCGuy Jul 29 '19 at 15:51
1

For those who doesn't use default and has the same issue, please check the hash name versus the function name in the index file.

Ahmed El Damasy
  • 671
  • 6
  • 13