{"record":{"id":"7d4c97ded7928b31","repo":"parcel-bundler/parcel","slug":"expected-package-json-contents-to-be-an-object","errorCode":null,"errorMessage":"Expected package.json contents to be an object.","messagePattern":"Expected package\\.json contents to be an object\\.","errorType":"exception","errorClass":"ThrowableDiagnostic","httpStatus":null,"severity":"error","filePath":"packages/core/package-manager/src/utils.js","lineNumber":67,"sourceCode":"  }\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 = [];\n  for (let field of ['dependencies', 'devDependencies', 'peerDependencies']) {\n    if (\n      typeof pkg[field] === 'object' &&\n      pkg[field] != null &&\n      pkg[field][name] != null\n    ) {\n      fields.push(field);\n    }\n  }\n","sourceCodeStart":49,"sourceCodeEnd":85,"githubUrl":"https://github.com/parcel-bundler/parcel/blob/59484858a1a0bcbb71f74088956bb437a2db6505/packages/core/package-manager/src/utils.js#L49-L85","documentation":"Thrown by getConflictingLocalDependencies() in utils.js when package.json parses as valid JSON but the result is not an object — it could be an array, string, number, boolean, or null. The check is `typeof pkg !== 'object' || pkg == null`, which catches arrays (typeof 'object' but not a plain object), null (typeof 'object'), strings, numbers, and booleans. This fires AFTER JSON.parse succeeds, so the JSON is syntactically valid but semantically wrong for a package.json.","triggerScenarios":"A package.json file containing valid JSON that is not an object literal — for example `[]` (array), `\"some-string\"`, `42`, `true`, or `null`. JSON.parse succeeds on these, but the typeof/null check catches them. The subsequent code that iterates ['dependencies', 'devDependencies', 'peerDependencies'] fields would fail without this guard.","commonSituations":"An empty package.json containing just `[]` or `null` (sometimes created by accident). A package.json that was overwritten with a JSON array (e.g., a config file accidentally saved as package.json). A build tool that generates package.json content as a non-object structure.","solutions":["Open package.json and ensure it contains a JSON object literal `{ ... }` at the top level.","If the file is empty, replace it with a minimal valid object: `{}`.","Check if a tool or script is generating malformed package.json content.","Validate with: `node -e \"const p=require('./package.json'); if(typeof p!=='object'||p===null||Array.isArray(p)) throw new Error('not an object')\"`."],"exampleFix":"// before: package.json contains\n[]\n\n// after: package.json contains\n{\n  \"name\": \"my-package\",\n  \"version\": \"1.0.0\"\n}","handlingStrategy":"validation","validationCode":"// Validate package.json is an object before use\nconst fs = require('fs');\n\nfunction validatePackageJsonObject(filePath) {\n  let pkg = JSON.parse(fs.readFileSync(filePath, 'utf8'));\n  if (typeof pkg !== 'object' || pkg === null || Array.isArray(pkg)) {\n    throw new Error(`Expected ${filePath} to contain a JSON object, got ${Array.isArray(pkg) ? 'array' : typeof pkg}`);\n  }\n  return pkg;\n}","typeGuard":"// Type guard for parsed package.json\nfunction isValidPackageJson(pkg) {\n  return typeof pkg === 'object' &&\n    pkg !== null &&\n    !Array.isArray(pkg);\n}","tryCatchPattern":"try {\n  await packageManager.resolve(id, from);\n} catch (e) {\n  if (e.diagnostics?.[0]?.message?.includes('Expected package.json contents to be an object')) {\n    // Fix the package.json to be an object literal\n    console.error('package.json must be a JSON object, not an array or primitive');\n  } else {\n    throw e;\n  }\n}","preventionTips":["Ensure package.json starts with `{` and ends with `}`.","Never overwrite package.json with array or primitive content.","Add a CI check that validates package.json structure: `node -e \\\"const p=require('./package.json'); if(typeof p!=='object'||!p||Array.isArray(p)) process.exit(1)\\\"`."],"tags":["package-json","type-validation","config","invalid-format"],"backgroundTag":null,"analyzedSha":"59484858a1a0bcbb71f74088956bb437a2db6505","analyzedAt":"2026-08-13T04:06:35.925Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}