angular/angular-cli · error · Error

Failed to parse " " as JSON AST Object. at location: .

Error message

Failed to parse "${path}" as JSON AST Object. ${printParseErrorCode(error)} at location: ${offset}.

What it means

`formatError` in the CLI's JSON-file utility converts the first `ParseError` reported by the `jsonc-parser` tokenizer into a descriptive `Error`, including the file path, the parser error code text (`printParseErrorCode`), and the byte offset where parsing failed. It is thrown whenever a JSON file the CLI reads (workspace config, etc.) is not syntactically valid JSON.

Solutions

  1. Open the file and fix the syntax at the reported offset (the message gives the location).
  2. Run a validator: `jq . <file>` or `npx jsonlint <file>` to pinpoint the problem.
  3. Restore from version control: `git checkout -- angular.json`.
  4. If you edit configs programmatically, validate JSON before saving (e.g. `JSON.parse` round-trip in a pre-commit hook).

Example fix

// before
{ "version": 1, "projects": { } // missing closing brace
// after
{ "version": 1, "projects": { } }
Defensive patterns

Strategy: validation

Validate before calling

import { readFile } from 'node:fs/promises';
const text = await readFile(path, 'utf8');
try { JSON.parse(text); } catch (e) {
  throw new Error(`${path} is not valid JSON; fix syntax before the CLI reads it.`, { cause: e });
}

Type guard

function parsesAsJson(text: string): boolean {
  try { JSON.parse(text); return true; } catch { return false; }
}

Try / catch

try {
  await runNgCommand();
} catch (e) {
  if (e instanceof Error && e.message.includes('Failed to parse') && e.message.includes('as JSON AST Object')) {
    // message contains file, error code, and offset — fix the syntax there
  } else { throw e; }
}

Prevention

When it happens

Trigger: Reading a JSON file through the CLI's JSON utilities (e.g. `readAndParseJson`, `JsonAst`) when `jsonc-parser`'s `parse`/`parseTree` reports errors — invalid syntax, unexpected characters, truncated file.

Common situations: Hand-edited `angular.json` with typos or trailing commas; merge-conflict markers (`<<<<<<<`) left in the config; a tool writing partial/truncated JSON; HTML error pages saved where a JSON file was expected.

Understand the failure class

Background: JSON parse error: "Unexpected token" / "not valid JSON" / "failed to parse" — what JSON parsers are really complaining about — this error's family across 45 libraries.

Related errors


AI-assisted analysis of angular/angular-cli@bb72145f9a (2026-08-30). Data as JSON: /api/errors/927973a50065ce0e. Report an issue: GitHub.

Appendix: source

Thrown at packages/angular/cli/src/utilities/json-file.ts:213

 */
export function readAndParseJson<T extends JsonValue>(path: string): T {
  const errors: ParseError[] = [];
  const content = parse(readFileSync(path, 'utf-8'), errors, { allowTrailingComma: true }) as T;
  if (errors.length) {
    formatError(path, errors);
  }

  return content;
}

/**
 * Formats a JSON parsing error and throws an exception.
 * @param path The path to the file that failed to parse.
 * @param errors The list of parsing errors.
 */
function formatError(path: string, errors: ParseError[]): never {
  const { error, offset } = errors[0];
  throw new Error(
    `Failed to parse "${path}" as JSON AST Object. ${printParseErrorCode(
      error,
    )} at location: ${offset}.`,
  );
}

/**
 * Parses a JSON string, supporting comments and trailing commas.
 * @param content The JSON string to parse.
 * @returns The parsed JSON object.
 */
export function parseJson<T extends JsonValue>(content: string): T {
  return parse(content, undefined, { allowTrailingComma: true }) as T;
}

View on GitHub (pinned to bb72145f9a)