cypress-io/cypress · error · Error

Failed to parse "${this.path}" as JSON AST Object. ${printPa

Error message

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

What it means

Thrown by the JSONFile.JsonAst getter when jsonc-parser's parseTree reports one or more ParseErrors against the file content. It surfaces the first error's code (human-readable via printParseErrorCode) and its byte offset, so you can locate the JSON syntax fault. The parser allows trailing commas, so the remaining fault classes are genuine syntax errors: unquoted keys, stray characters, unterminated strings, or a dangling comma in a position the parser tolerates differently.

Source

Thrown at npm/cypress-schematic/src/schematics/utils/jsonFile.ts:52

      this.content = buffer.toString()
    } else {
      throw new Error(`Could not read '${path}'.`)
    }
  }

  private _jsonAst: Node | undefined
  private get JsonAst (): Node | undefined {
    if (this._jsonAst) {
      return this._jsonAst
    }

    const errors: ParseError[] = []

    this._jsonAst = parseTree(this.content, errors, { allowTrailingComma: true })
    if (errors.length) {
      const { error, offset } = errors[0]

      throw new Error(
         `Failed to parse "${this.path}" as JSON AST Object. ${printParseErrorCode(
           error,
         )} at location: ${offset}.`,
      )
    }

    return this._jsonAst
  }

  get (jsonPath: JSONPath): unknown {
    const jsonAstNode = this.JsonAst

    if (!jsonAstNode) {
      return undefined
    }

    if (jsonPath.length === 0) {
      return getNodeValue(jsonAstNode)

View on GitHub (pinned to 0d85fdc912)

Solutions

  1. Open the file named in `this.path` and navigate to the reported byte `offset` to find the syntax fault.
  2. Validate the file with `npx json5 <file>` or `node -e "JSON.parse(require('fs').readFileSync('<file>'))"` to surface the precise error.
  3. Fix the JSON (quote keys, remove comments, balance braces); remember trailing commas are allowed here but JSON5-only constructs like unquoted keys are not.
  4. Re-run `ng add @cypress/schematic` once the file parses cleanly.

Example fix

// before: tsconfig.json with a comment and unquoted key
{
  // compiler options
  compilerOptions: { "target": "es2020", }
}
// after: valid JSON
{
  "compilerOptions": { "target": "es2020" }
}
Defensive patterns

Strategy: validation

Validate before calling

function safeParse(content: string, path: string) {
  const errors: ParseError[] = []
  parseTree(content, errors, { allowTrailingComma: true })
  if (errors.length) throw new Error(`${path}: ${printParseErrorCode(errors[0].error)} @${errors[0].offset}`)
}

Type guard

const isValidJsonContent = (s: string): boolean => { const e: ParseError[] = []; parseTree(s, e, { allowTrailingComma: true }); return e.length === 0 }

Try / catch

try {
  jsonFile.get(['key'])
} catch (e) {
  if (e.message.startsWith('Failed to parse')) { /* run a JSON linter, report offset to user */ }
  else throw e
}

Prevention

When it happens

Trigger: Calling JSONFile.get(jsonPath) (or any access that touches JsonAst) when the loaded file content contains invalid JSON. parseTree is invoked with { allowTrailingComma: true }, so trailing commas are tolerated; any other syntax violation populates the errors array and triggers the throw.

Common situations: A hand-edited package.json/angular.json/tsconfig.json with a typo (missing quote, extra brace, comment in a strict JSON file), a merge conflict left unresolved inside a JSON file, or a file corrupted by an editor/plugin that inserted BOM or non-JSON. The schematic runs against these files during `ng add`.

Understand the failure class

Related errors


AI-assisted analysis of cypress-io/cypress@0d85fdc912 (2026-08-12). Data as JSON: /api/errors/08d98de86f064e66. Report an issue: GitHub.