angular/angular-cli · error
Failed to parse "${path}" as JSON AST Object. ${printParseEr
Error message
Failed to parse "${path}" as JSON AST Object. ${printParseErrorCode(error)} at location: ${offset}. What it means
readJsonFile parses the file with the JSON AST parser (jsonc-parser `parse`) collecting syntax errors; if any errors are reported it throws an Error naming the file, the parse error code, and the character offset. Raised when the JSON file exists but is malformed.
Source
Thrown at packages/angular_devkit/schematics/tools/file-system-utility.ts:30
import { FileDoesNotExistException } from '../src/exception/exception';
export function readJsonFile(path: string): JsonValue {
let data;
try {
data = readFileSync(path, 'utf-8');
} catch (e) {
if (e && typeof e === 'object' && 'code' in e && e.code === 'ENOENT') {
throw new FileDoesNotExistException(path);
}
throw e;
}
const errors: ParseError[] = [];
const content = parse(data, errors, { allowTrailingComma: true }) as JsonValue;
if (errors.length) {
const { error, offset } = errors[0];
throw new Error(
`Failed to parse "${path}" as JSON AST Object. ${printParseErrorCode(
error,
)} at location: ${offset}.`,
);
}
return content;
}
View on GitHub (pinned to bb72145f9a)
Solutions
- Use the offset in the message to find the exact broken location in the file (count characters or use an editor's Go to Offset).
- Fix the JSON syntax error (missing comma/quote/brace, conflict markers, placeholder text).
- Validate the file with `node -e "JSON.parse(require('fs').readFileSync('<path>','utf8'))"` or an editor JSON linter before re-running.
- If generated, regenerate the file from source instead of patching the output.
Example fix
// before (collection.json)
{ "schematics": { "a": { "factory": "./a#index" } "description": "x" } }
// after (comma added)
{ "schematics": { "a": { "factory": "./a#index", "description": "x" } } } Defensive patterns
Strategy: validation
Validate before calling
try {
JSON.parse(readFileSync(path, 'utf8'));
} catch (e) {
throw new Error(`${path} is not valid JSON: ${(e as Error).message}`);
} Try / catch
try {
const json = readJsonFile(path);
} catch (e) {
if (String(e.message).startsWith('Failed to parse')) {
const m = e.message.match(/location: (\d+)/);
console.error(`JSON syntax error in ${path} near offset ${m?.[1]} — fix and retry`);
process.exitCode = 1;
} else throw e;
} Prevention
- Enable JSON linting/format-on-save in editors and validate all JSON in CI.
- Search files for merge conflict markers (<<<<<<<) before committing.
- Never hand-write JSON with comments or trailing commas outside parser-supported settings; prefer jsonc only where supported.
When it happens
Trigger: readJsonFile on a file whose contents are invalid JSON — parse() pushes into `errors` and the first error is thrown with printParseErrorCode and the offset.
Common situations: Hand-edited collection.json/schema.json with a missing comma, unquoted key, or stray bracket; a merge conflict marker left in the file; a template placeholder ({{ }}) unrendered in a JSON file; saving a JSON5 feature (comments) unsupported by strict parse settings.
Understand the failure class
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- Failed to parse package manager output: ${e instanceof Error
- Collection JSON at path ${JSON.stringify(path)} is invalid.
- Invalid config found at ${workspace.filePath}. CLI should be
- Invalid value for argument: ${key}, Given: '${pair}', Expect
- Invalid JSON path.
AI-assisted analysis of angular/angular-cli@bb72145f9a (2026-08-30).
Data as JSON: /api/errors/cb484d0c9eef7788.
Report an issue: GitHub.