angular/angular-cli · error · FactoryCannotBeResolvedException
Schematic ${JSON.stringify(name)} cannot resolve the factory
Error message
Schematic ${JSON.stringify(name)} cannot resolve the factory. What it means
Thrown by createSchematicDescription when the `factory` string is present but _resolveReferenceString cannot load it — the module or exported function does not exist at the referenced path. The declaration is valid JSON but the code it points to cannot be resolved.
Source
Thrown at packages/angular_devkit/schematics/tools/file-system-engine-host-base.ts:246
const extendCollection = this.createCollectionDescription(collectionName);
return this.createSchematicDescription(schematicName, extendCollection);
} else {
return this.createSchematicDescription(schematicName, collection);
}
}
// Use any on this ref as we don't have the OptionT here, but we don't need it (we only need
// the path).
if (!partialDesc.factory) {
throw new SchematicMissingFactoryException(name);
}
const resolvedRef = this._resolveReferenceString(
partialDesc.factory,
collectionPath,
collection,
);
if (!resolvedRef) {
throw new FactoryCannotBeResolvedException(name);
}
let schema = partialDesc.schema;
let schemaJson: JsonObject | undefined = undefined;
if (schema) {
if (!isAbsolute(schema)) {
schema = join(collectionPath, schema);
}
schemaJson = readJsonFile(schema) as JsonObject;
}
// The schematic path is used to resolve URLs.
// We should be able to just do `dirname(resolvedRef.path)` but for compatibility with
// Bazel under Windows this directory needs to be resolved from the collection instead.
// This is needed because on Bazel under Windows the data files (such as the collection or
// url files) are not in the same place as the compiled JS.
const maybePath = join(collectionPath, partialDesc.factory);
const path =View on GitHub (pinned to bb72145f9a)
Solutions
- Check the factory path and exported name in collection.json against the actual file and its exports.
- Compile the TypeScript sources (tsc) if the factory lives in uncompiled .ts files.
- If publishing, ensure the factory files are included in package.json `files` and shipped to npm.
- Ensure the module's dependencies are installed (node_modules present at the collection root).
Example fix
// before (collection.json)
"my-schematic": { "factory": "./src/index#generate" }
// after (path matches compiled output and export name)
"my-schematic": { "factory": "./my-schematic/index#mySchematicGenerator" } Defensive patterns
Strategy: validation
Validate before calling
import { existsSync } from 'fs';
const coll = JSON.parse(readFileSync('collection.json', 'utf8'));
for (const [name, s] of Object.entries(coll.schematics)) {
if (s.factory) {
const [file, fn] = String(s.factory).split('#');
if (!existsSync(require.resolve(file, { paths: [collectionDir] }))) throw new Error(`Schematic '${name}': ${file} not found`);
}
} Try / catch
try {
const schematic = collection.createSchematic(name, true);
} catch (e) {
if (String(e.message).includes('cannot resolve the factory')) {
console.error('Check the factory path/export and compile TS before running');
} else throw e;
} Prevention
- Always build (tsc) the collection before pointing tools at it; wire build into prepublishOnly.
- Smoke-test every schematic with SchematicTestRunner in CI, which resolves factories like production does.
- Include compiled factory files in the npm package (files field); avoid referencing src/ directly.
When it happens
Trigger: collection.json `factory` value like "./foo/index#bar" where ./foo/index does not exist, is not compiled, or does not export `bar`; _resolveReferenceString returns null and FactoryCannotBeResolvedException is thrown.
Common situations: Running a TypeScript collection without compiling to JS first; a refactor renamed the factory function without updating collection.json; a wrong relative path after moving files; publishing a package without the factory files included in the npm package.
Related errors
- Unknown schematics built-in module '${id}' requested from sc
- Option "project" is required.
- Project is not defined in this workspace.
- Targets are not defined for this project.
- Circular collection reference "${name}".
AI-assisted analysis of angular/angular-cli@bb72145f9a (2026-08-30).
Data as JSON: /api/errors/521be2ca0e927c72.
Report an issue: GitHub.