{"record":{"id":"b11eb2fd397ea5c7","repo":"yarnpkg/yarn","slug":"error-parsing-json-at-0-1","errorCode":null,"errorMessage":"Error parsing JSON at $0, $1.","messagePattern":"Error parsing JSON at \\$0, \\$1\\.","errorType":"validation","errorClass":"MessageError","httpStatus":null,"severity":"error","filePath":"src/config.js","lineNumber":970,"sourceCode":"          object[field] = sortObject(object[field]);\n        }\n      }\n\n      await fs.writeFilePreservingEol(loc, JSON.stringify(object, null, indent || constants.DEFAULT_INDENT) + '\\n');\n    }\n  }\n\n  /**\n   * Call the passed factory (defaults to fs.readJson) and rethrow a pretty error message if it was the result\n   * of a syntax error.\n   */\n\n  readJson(loc: string, factory: (filename: string) => Promise<Object> = fs.readJson): Promise<Object> {\n    try {\n      return factory(loc);\n    } catch (err) {\n      if (err instanceof SyntaxError) {\n        throw new MessageError(this.reporter.lang('jsonError', loc, err.message));\n      } else {\n        throw err;\n      }\n    }\n  }\n\n  static async create(opts: ConfigOptions = {}, reporter: Reporter = new NoopReporter()): Promise<Config> {\n    const config = new Config(reporter);\n    await config.init(opts);\n    return config;\n  }\n}\n\nexport function extractWorkspaces(manifest: ?Manifest): ?WorkspacesConfig {\n  if (!manifest || !manifest.workspaces) {\n    return undefined;\n  }\n","sourceCodeStart":952,"sourceCodeEnd":988,"githubUrl":"https://github.com/yarnpkg/yarn/blob/c2dda503f3759b5be5f0e24ecd9cf5c97a540147/src/config.js#L952-L988","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before (package.json — trailing comma)\n{\n  \"name\": \"app\",\n  \"version\": \"1.0.0\",\n}\n// after\n{\n  \"name\": \"app\",\n  \"version\": \"1.0.0\"\n}","handlingStrategy":"try-catch","validationCode":"const fs = require('fs');\nfunction validateJsonFile(filePath) {\n  try {\n    JSON.parse(fs.readFileSync(filePath, 'utf8'));\n    return true;\n  } catch (err) {\n    console.error(`Invalid JSON in ${filePath}: ${err.message}`);\n    return false;\n  }\n}\n// validate package.json before running yarn\nvalidateJsonFile('package.json');","typeGuard":"function isParsableJson(text) {\n  try { JSON.parse(text); return true; } catch { return false; }\n}","tryCatchPattern":"try {\n  return await config.readJson(loc, factory);\n} catch (err) {\n  if (err instanceof MessageError && err.message.startsWith('Error parsing JSON')) {\n    reporter.error(`Fix JSON syntax in ${loc}, then re-run.`);\n    throw err;\n  }\n  throw err;\n}","preventionTips":["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."],"tags":["json","config","manifest","parse-error"],"backgroundTag":null,"analyzedSha":"c2dda503f3759b5be5f0e24ecd9cf5c97a540147","analyzedAt":"2026-08-13T04:17:06.305Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}