angular/angular-cli · error
Failed to parse "${path}" as JSON. ${printParseErrorCode(err
Error message
Failed to parse "${path}" as JSON. ${printParseErrorCode(error)} at offset: ${offset}. What it means
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.
Source
Thrown at packages/angular_devkit/schematics/src/tree/host-tree.ts:330
if (
e instanceof TypeError ||
(e as NodeJS.ErrnoException).code === 'ERR_ENCODING_INVALID_ENCODED_DATA'
) {
throw new Error(`Failed to decode "${path}" as UTF-8 text.`, { cause: e });
}
throw e;
}
}
readJson(path: string): JsonValue {
const content = this.readText(path);
const errors: ParseError[] = [];
const result = jsoncParse(content, errors, { allowTrailingComma: true });
// If there is a parse error throw with the error information
if (errors[0]) {
const { error, offset } = errors[0];
throw new Error(
`Failed to parse "${path}" as JSON. ${printParseErrorCode(error)} at offset: ${offset}.`,
);
}
return result;
}
exists(path: string): boolean {
return this._recordSync.isFile(this._normalizePath(path));
}
get(path: string): FileEntry | null {
const p = this._normalizePath(path);
if (this._recordSync.isDirectory(p)) {
throw new PathIsDirectoryException(p);
}
if (!this._recordSync.exists(p)) {
return null;View on GitHub (pinned to bb72145f9a)
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.
Example fix
// before
const config = tree.readJson('/project/angular.json');
// after
let config;
try {
config = tree.readJson('/project/angular.json');
} catch (e) {
if (String(e).startsWith('Failed to parse')) {
throw new Error('angular.json is invalid JSON — fix the syntax at the reported offset');
}
throw e;
} Defensive patterns
Strategy: try-catch
Validate before calling
const raw = tree.readText(path);
const errors: ParseError[] = [];
jsoncParse(raw, errors, { allowTrailingComma: true });
const isValidJson = errors.length === 0; Type guard
function parsesAsJson(text: string): boolean {
try { JSON.parse(text.replace(/^\uFEFF/, '')); return true; } catch { return false; }
} Try / catch
try {
const json = tree.readJson(path);
} catch (e) {
if ((e as Error).message.startsWith('Failed to parse')) {
// fall back to defaults or surface a friendly config error
} else throw e;
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
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.
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- Failed to parse "${path}" as JSON AST Object. ${printParseEr
- Invalid config found at ${workspace.filePath}. CLI should be
- Workspace config file cannot be loaded: ${configPath}
- ${message}
- Collection JSON at path ${JSON.stringify(path)} is invalid.
AI-assisted analysis of angular/angular-cli@bb72145f9a (2026-08-30).
Data as JSON: /api/errors/b003b3ea41fb9e7a.
Report an issue: GitHub.