{"record":{"id":"e01825a397086ccf","repo":"angular/angular-cli","slug":"invalid-collection-json-schematics-needs-to-be-an","errorCode":null,"errorMessage":"Invalid collection.json; schematics needs to be an object.","messagePattern":"Invalid collection\\.json; schematics needs to be an object\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/angular_devkit/schematics_cli/blank/factory.ts","lineNumber":34,"sourceCode":"  applyTemplates,\n  chain,\n  mergeWith,\n  move,\n  url,\n} from '@angular-devkit/schematics';\nimport { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';\nimport { Schema } from './schema';\n\nfunction addSchematicToCollectionJson(\n  collectionPath: Path,\n  schematicName: string,\n  description: JsonObject,\n): Rule {\n  return (tree: Tree) => {\n    const collectionJson = tree.readJson(collectionPath);\n\n    if (!isJsonObject(collectionJson) || !isJsonObject(collectionJson.schematics)) {\n      throw new Error('Invalid collection.json; schematics needs to be an object.');\n    }\n\n    collectionJson['schematics'][schematicName] = description;\n    tree.overwrite(collectionPath, JSON.stringify(collectionJson, undefined, 2));\n  };\n}\n\nexport default function (options: Schema): Rule {\n  const schematicsVersion = require('@angular-devkit/schematics/package.json').version;\n  const coreVersion = require('@angular-devkit/core/package.json').version;\n\n  // Verify if we need to create a full project, or just add a new schematic.\n  return (tree: Tree, context: SchematicContext) => {\n    if (!options.name) {\n      throw new SchematicsException('name option is required.');\n    }\n\n    let collectionPath: Path | undefined;","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/angular/angular-cli/blob/bb72145f9ab45aee29f523236b3a25cd0813a841/packages/angular_devkit/schematics_cli/blank/factory.ts#L16-L52","documentation":"The blank schematic factory's addSchematicToCollectionJson rule reads the target collection.json from the virtual Tree and requires both the root document and its 'schematics' property to be JSON objects before inserting the new schematic entry. This error is thrown when either check fails, because the factory cannot safely write schematics['<name>'] into a malformed collection file. It is a deliberate sanity guard so the schematic never corrupts or misinterprets an invalid collection manifest.","triggerScenarios":"Running the blank schematic against a collection.json that is not a JSON object (e.g. a JSON array or scalar) or whose 'schematics' field is missing or not an object (e.g. an array, string, or absent). This happens inside addSchematicToCollectionJson when updating an existing collection.","commonSituations":"Hand-edited collection.json that lost the 'schematics' key or wrapped schematics in the wrong shape; a collection.json copied from a different schema (e.g. builder or migration collections); trailing corruption or a placeholder file created before fields were filled in.","solutions":["Open collection.json and ensure the root is a JSON object containing a 'schematics': { ... } object, then rerun the schematic.","If the file is for a different purpose or corrupted beyond repair, restore a valid collection.json (e.g. from version control) with a schematics object.","If you only meant to create a brand-new project, remove the pre-existing collection.json so the blank schematic generates a fresh, valid one instead of augmenting it."],"exampleFix":"// before (collection.json)\n{\n  \"schematics\": []\n}\n\n// after\n{\n  \"schematics\": {}\n}","handlingStrategy":"validation","validationCode":"const fs = require('fs');\nfunction collectionJsonIsValid(path) {\n  const json = JSON.parse(fs.readFileSync(path, 'utf8'));\n  return typeof json === 'object' && json !== null && !Array.isArray(json) &&\n    typeof json.schematics === 'object' && json.schematics !== null && !Array.isArray(json.schematics);\n}\nif (fs.existsSync('collection.json') && !collectionJsonIsValid('collection.json')) {\n  throw new Error('collection.json must be an object with an object-valued \"schematics\" field');\n}","typeGuard":"function isJsonObject(v) {\n  return typeof v === 'object' && v !== null && !Array.isArray(v);\n}","tryCatchPattern":"try {\n  await schematicRunner.executeBlank({ name });\n} catch (err) {\n  if (err.message.includes('schematics needs to be an object')) {\n    // repair or regenerate collection.json, then retry\n  } else { throw err; }\n}","preventionTips":["Validate collection.json against the schematics collection schema before running generators.","Never wrap 'schematics' in an array; always keep it as a plain object keyed by schematic name.","Keep collection.json in version control so hand-edit mistakes can be reverted.","Run with --dry-run first when pointing a schematic at an existing collection."],"tags":["schematics","json","validation","angular-cli"],"backgroundTag":"schema-validation-failed","analyzedSha":"bb72145f9ab45aee29f523236b3a25cd0813a841","analyzedAt":"2026-08-30T02:47:34.745Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}