{"record":{"id":"b003b3ea41fb9e7a","repo":"angular/angular-cli","slug":"failed-to-parse-path-as-json-printparseerr","errorCode":null,"errorMessage":"Failed to parse \"${path}\" as JSON. ${printParseErrorCode(error)} at offset: ${offset}.","messagePattern":"Failed to parse \"(.+?)\" as JSON\\. (.+?) at offset: (.+?)\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/angular_devkit/schematics/src/tree/host-tree.ts","lineNumber":330,"sourceCode":"      if (\n        e instanceof TypeError ||\n        (e as NodeJS.ErrnoException).code === 'ERR_ENCODING_INVALID_ENCODED_DATA'\n      ) {\n        throw new Error(`Failed to decode \"${path}\" as UTF-8 text.`, { cause: e });\n      }\n      throw e;\n    }\n  }\n\n  readJson(path: string): JsonValue {\n    const content = this.readText(path);\n    const errors: ParseError[] = [];\n    const result = jsoncParse(content, errors, { allowTrailingComma: true });\n\n    // If there is a parse error throw with the error information\n    if (errors[0]) {\n      const { error, offset } = errors[0];\n      throw new Error(\n        `Failed to parse \"${path}\" as JSON. ${printParseErrorCode(error)} at offset: ${offset}.`,\n      );\n    }\n\n    return result;\n  }\n\n  exists(path: string): boolean {\n    return this._recordSync.isFile(this._normalizePath(path));\n  }\n\n  get(path: string): FileEntry | null {\n    const p = this._normalizePath(path);\n    if (this._recordSync.isDirectory(p)) {\n      throw new PathIsDirectoryException(p);\n    }\n    if (!this._recordSync.exists(p)) {\n      return null;","sourceCodeStart":312,"sourceCodeEnd":348,"githubUrl":"https://github.com/angular/angular-cli/blob/bb72145f9ab45aee29f523236b3a25cd0813a841/packages/angular_devkit/schematics/src/tree/host-tree.ts#L312-L348","documentation":"Thrown by HostTree.readJson when jsonc-parser reports a parse error while parsing the file's text content (trailing commas are allowed). The error includes the parser error code description and the character offset of the first error. It indicates the file content is not syntactically valid JSON/JSONC.","triggerScenarios":"Calling tree.readJson(path) on a file containing malformed JSON: missing/extra commas or braces, unquoted or single-quoted keys, comments (unless JSONC-valid), truncated content, or a non-JSON file given a .json path.","commonSituations":"Hand-edited tsconfig/package/angular.json with a syntax mistake; a template-merged config producing invalid output; reading a .json file that is actually JSON5 or contains BOM issues.","solutions":["Open the file and fix the JSON syntax at the reported offset (the message names the exact position).","Validate the file with a JSON linter/parser (e.g. `node -e \"JSON.parse(...)\"` or a JSONC-aware validator) before running the schematic.","If the file may legitimately be non-JSON, wrap readJson in try/catch and provide a default or fall back to readText/manual parsing.","Strip a UTF-8 BOM before parsing if the file was saved with BOM."],"exampleFix":"// before\nconst config = tree.readJson('/project/angular.json');\n// after\nlet config;\ntry {\n  config = tree.readJson('/project/angular.json');\n} catch (e) {\n  if (String(e).startsWith('Failed to parse')) {\n    throw new Error('angular.json is invalid JSON — fix the syntax at the reported offset');\n  }\n  throw e;\n}","handlingStrategy":"try-catch","validationCode":"const raw = tree.readText(path);\nconst errors: ParseError[] = [];\njsoncParse(raw, errors, { allowTrailingComma: true });\nconst isValidJson = errors.length === 0;","typeGuard":"function parsesAsJson(text: string): boolean {\n  try { JSON.parse(text.replace(/^\\uFEFF/, '')); return true; } catch { return false; }\n}","tryCatchPattern":"try {\n  const json = tree.readJson(path);\n} catch (e) {\n  if ((e as Error).message.startsWith('Failed to parse')) {\n    // fall back to defaults or surface a friendly config error\n  } else throw e;\n}","preventionTips":["Lint config JSON files (tsconfig/angular.json/package.json) in CI","Strip BOMs before parsing","Use JSONC-aware editors/format-on-save for config files","Prefer readJson only on files known to be JSON; use readText + tolerant parse otherwise"],"tags":["json","parse-error","config","schematics"],"backgroundTag":"json-parse-error","analyzedSha":"bb72145f9ab45aee29f523236b3a25cd0813a841","analyzedAt":"2026-08-30T02:47:34.745Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}