yarnpkg/yarn · error · MessageError
Error parsing JSON at $0, $1.
Error message
Error parsing JSON at $0, $1.
What it means
Thrown by `readJson` when the underlying factory (default `fs.readJson`, or `fs.readJsonAndFile`) throws a `SyntaxError`. Yarn catches it and rethrows as a `MessageError` (`jsonError`) with the file path (`$0`) and the original parse message (`$1`) so the user knows exactly which JSON file is malformed.
Source
Thrown at src/config.js:970
object[field] = sortObject(object[field]);
}
}
await fs.writeFilePreservingEol(loc, JSON.stringify(object, null, indent || constants.DEFAULT_INDENT) + '\n');
}
}
/**
* Call the passed factory (defaults to fs.readJson) and rethrow a pretty error message if it was the result
* of a syntax error.
*/
readJson(loc: string, factory: (filename: string) => Promise<Object> = fs.readJson): Promise<Object> {
try {
return factory(loc);
} catch (err) {
if (err instanceof SyntaxError) {
throw new MessageError(this.reporter.lang('jsonError', loc, err.message));
} else {
throw err;
}
}
}
static async create(opts: ConfigOptions = {}, reporter: Reporter = new NoopReporter()): Promise<Config> {
const config = new Config(reporter);
await config.init(opts);
return config;
}
}
export function extractWorkspaces(manifest: ?Manifest): ?WorkspacesConfig {
if (!manifest || !manifest.workspaces) {
return undefined;
}
View on GitHub (pinned to c2dda503f3)
Solutions
- Open the file path reported in `$0` and fix the JSON syntax error described in `$1`.
- Validate with a JSON linter: `node -e "JSON.parse(require('fs').readFileSync('<file>'))"`.
- If it is a cache metadata file, run `yarn cache clean`.
- Resolve git merge-conflict markers in the file before running Yarn.
Example fix
// before (package.json — trailing comma)
{
"name": "app",
"version": "1.0.0",
}
// after
{
"name": "app",
"version": "1.0.0"
} Defensive patterns
Strategy: try-catch
Validate before calling
const fs = require('fs');
function validateJsonFile(filePath) {
try {
JSON.parse(fs.readFileSync(filePath, 'utf8'));
return true;
} catch (err) {
console.error(`Invalid JSON in ${filePath}: ${err.message}`);
return false;
}
}
// validate package.json before running yarn
validateJsonFile('package.json'); Type guard
function isParsableJson(text) {
try { JSON.parse(text); return true; } catch { return false; }
} Try / catch
try {
return await config.readJson(loc, factory);
} catch (err) {
if (err instanceof MessageError && err.message.startsWith('Error parsing JSON')) {
reporter.error(`Fix JSON syntax in ${loc}, then re-run.`);
throw err;
}
throw err;
} Prevention
- Use a JSON-aware editor or schema linter (e.g. VS Code JSON validation) for package.json.
- Never leave merge-conflict markers in JSON files.
- Validate JSON files in a precommit hook before they reach Yarn.
When it happens
Trigger: Any JSON file Yarn reads is malformed: package.json with a trailing comma or comment, .yarnrc.json, yarn-metadata.json in the cache, or a registry response written to disk. Reached during `readJson` calls in config, manifest reading, and lockfile JSON operations.
Common situations: Hand-editing package.json and leaving a syntax error (unquoted key, trailing comma). A JSON5-style comment accidentally saved into package.json. Corrupted cache metadata after a crash. Merge-conflict markers left in package.json.
Related errors
- unknownPackageName
- unknownPackageName
- workspaceRootNotFound
- workspaceRootNotFound
- The workspaces field in package.json must be an array.
AI-assisted analysis of yarnpkg/yarn@c2dda503f3 (2026-08-13).
Data as JSON: /api/errors/b11eb2fd397ea5c7.
Report an issue: GitHub.