{"record":{"id":"0c82f9d11e337ca2","repo":"parcel-bundler/parcel","slug":"failed-to-parse-package-json","errorCode":null,"errorMessage":"Failed to parse package.json","messagePattern":"Failed to parse package\\.json","errorType":"exception","errorClass":"ThrowableDiagnostic","httpStatus":null,"severity":"error","filePath":"packages/core/package-manager/src/utils.js","lineNumber":57,"sourceCode":"\nexport async function getConflictingLocalDependencies(\n  fs: FileSystem,\n  name: string,\n  local: FilePath,\n  projectRoot: FilePath,\n): Promise<?{|json: string, filePath: FilePath, fields: Array<string>|}> {\n  let pkgPath = await resolveConfig(fs, local, ['package.json'], projectRoot);\n  if (pkgPath == null) {\n    return;\n  }\n\n  let pkgStr = await fs.readFile(pkgPath, 'utf8');\n  let pkg;\n  try {\n    pkg = JSON.parse(pkgStr);\n  } catch (e) {\n    // TODO: codeframe\n    throw new ThrowableDiagnostic({\n      diagnostic: {\n        message: 'Failed to parse package.json',\n        origin: '@parcel/package-manager',\n      },\n    });\n  }\n\n  if (typeof pkg !== 'object' || pkg == null) {\n    // TODO: codeframe\n    throw new ThrowableDiagnostic({\n      diagnostic: {\n        message: 'Expected package.json contents to be an object.',\n        origin: '@parcel/package-manager',\n      },\n    });\n  }\n\n  let fields = [];","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/parcel-bundler/parcel/blob/59484858a1a0bcbb71f74088956bb437a2db6505/packages/core/package-manager/src/utils.js#L39-L75","documentation":"Thrown by getConflictingLocalDependencies() in utils.js when JSON.parse() fails on the contents of a package.json file. The function reads the file via fs.readFile, attempts JSON.parse, and catches any SyntaxError to re-throw as a ThrowableDiagnostic. The TODO comment notes that codeframes are not yet attached to this diagnostic.","triggerScenarios":"resolveConfig finds a package.json, fs.readFile reads its contents as UTF-8, and JSON.parse throws a SyntaxError due to invalid JSON syntax (trailing commas, unquoted keys, single quotes, comments, etc.). The catch block re-wraps the error as a diagnostic with origin '@parcel/package-manager'.","commonSituations":"A trailing comma in package.json (common copy-paste error). Unquoted property keys. JSON5 syntax (comments, single quotes) in a file expected to be standard JSON. A BOM character or encoding issue causing a parse failure. Manual editing of package.json introducing syntax errors. Merge conflicts leaving conflict markers (<<<<<<<) in the file.","solutions":["Validate the package.json file with a JSON linter or `node -e \"JSON.parse(require('fs').readFileSync('package.json','utf8'))\"`.","Fix the JSON syntax error — check for trailing commas, unquoted keys, or comments.","Remove merge conflict markers if present (<<<<<<<, =======, >>>>>>>).","Use an editor with JSON syntax checking to catch errors before saving."],"exampleFix":"// before: invalid package.json\n{\n  \"name\": \"myapp\",\n  \"dependencies\": {\n    \"lodash\": \"^4.0.0\",\n  },  // <-- trailing comma\n}\n\n// after: valid JSON\n{\n  \"name\": \"myapp\",\n  \"dependencies\": {\n    \"lodash\": \"^4.0.0\"\n  }\n}","handlingStrategy":"validation","validationCode":"// Validate package.json is parseable JSON before use\nconst fs = require('fs');\n\nfunction validatePackageJson(filePath) {\n  let contents = fs.readFileSync(filePath, 'utf8');\n  try {\n    JSON.parse(contents);\n  } catch (e) {\n    throw new Error(`Invalid JSON in ${filePath}: ${e.message}`);\n  }\n}\n\n// Run before Parcel build\nvalidatePackageJson('./package.json');","typeGuard":null,"tryCatchPattern":"try {\n  await packageManager.resolve(id, from);\n} catch (e) {\n  if (e.diagnostics?.[0]?.message === 'Failed to parse package.json') {\n    // Validate and fix the package.json file\n    let {execSync} = require('child_process');\n    execSync('node -e \"JSON.parse(require(\\'fs\\').readFileSync(\\'package.json\\',\\'utf8\\'))\"', {stdio: 'inherit'});\n  } else {\n    throw e;\n  }\n}","preventionTips":["Use a JSON schema validator or linter in your editor for package.json.","Add a pre-commit hook that validates JSON syntax: `node -e \\\"JSON.parse(require('fs').readFileSync('package.json','utf8'))\\\"`.","Never manually edit package.json while sleep-deprived — use `npm install <pkg>` instead.","Resolve git merge conflicts in package.json completely before committing."],"tags":["package-json","json-parse","syntax-error","config","validation"],"backgroundTag":null,"analyzedSha":"59484858a1a0bcbb71f74088956bb437a2db6505","analyzedAt":"2026-08-13T04:06:35.925Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}