{"record":{"id":"2cb87430bb20daeb","repo":"facebook/docusaurus","slug":"couldn-t-load-package-json-file-at-packagejsonpa","errorCode":null,"errorMessage":"Couldn't load package.json file at ${packageJsonPath}","messagePattern":"Couldn't load package\\.json file at (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/docusaurus/src/server/siteMetadata.ts","lineNumber":29,"sourceCode":"import type {\n  LoadedPlugin,\n  PluginVersionInformation,\n  SiteMetadata,\n} from '@docusaurus/types';\n\ntype PackageJson = {\n  name?: string;\n  version?: string;\n};\n\nasync function tryLoadPackageJson(\n  packageJsonPath: string,\n): Promise<PackageJson | undefined> {\n  if (await fs.pathExists(packageJsonPath)) {\n    try {\n      return (await fs.readJSON(packageJsonPath)) as PackageJson;\n    } catch (error) {\n      throw new Error(`Couldn't load package.json file at ${packageJsonPath}`, {\n        cause: error,\n      });\n    }\n  }\n  return undefined;\n}\n\nexport async function tryLoadSitePackageJson(\n  siteDir: string,\n): Promise<PackageJson | undefined> {\n  return tryLoadPackageJson(path.join(siteDir, 'package.json'));\n}\n\nexport async function loadPluginVersion(\n  pluginPath: string,\n  siteDir: string,\n): Promise<PluginVersionInformation> {\n  let potentialPluginPackageJsonDirectory = path.dirname(pluginPath);","sourceCodeStart":11,"sourceCodeEnd":47,"githubUrl":"https://github.com/facebook/docusaurus/blob/3f483e80e326cc646b54b83d564b3f0c4881b9a6/packages/docusaurus/src/server/siteMetadata.ts#L11-L47","documentation":"Thrown by `tryLoadPackageJson` when a `package.json` file exists at the given path but `fs.readJSON` fails to parse it. The original parse error is attached as `cause`. Docusaurus reads the site's `package.json` to derive site metadata (name/version), so a malformed file aborts startup.","triggerScenarios":"The site's `package.json` (or a plugin's) contains invalid JSON — trailing commas, comments, unquoted keys, or truncation. The `fs.pathExists` check passes but `fs.readJSON` throws; siteMetadata.ts:27-31 re-throws a clearer message.","commonSituations":"Hand-editing package.json and leaving a syntax error; a bot/tool writing JSON5 by mistake; a corrupted file from a git merge conflict; an npm script that overwrote package.json with log output.","solutions":["Validate the file: `node -e \"JSON.parse(require('fs').readFileSync('package.json','utf8'))\"` or use a JSON linter.","Fix the specific syntax error (the `cause` error carries the position).","Restore from version control if corrupted: `git checkout -- package.json`."],"exampleFix":"// before (package.json, invalid)\n{\n  \"name\": \"site\",\n  // comment is not valid JSON\n  \"version\": \"1.0.0\",\n}\n// after\n{\n  \"name\": \"site\",\n  \"version\": \"1.0.0\"\n}","handlingStrategy":"try-catch","validationCode":"function readPackageJsonSafe(p: string) {\n  try { return JSON.parse(fs.readFileSync(p, 'utf8')); }\n  catch (e) { throw new Error(`Invalid JSON in ${p}: ${(e as Error).message}`); }\n}","typeGuard":null,"tryCatchPattern":"try {\n  const pkg = await fs.readJSON(packageJsonPath);\n} catch (e) {\n  console.error('package.json parse failed:', e.message);\n  // restore from git or prompt user\n}","preventionTips":["Never hand-edit package.json with JSON5 syntax.","Lint package.json in CI (`node -e \"JSON.parse(...)\"`).","Commit package.json so a corrupt copy can be `git checkout`-ed."],"tags":["config","package-json","parsing","startup"],"backgroundTag":null,"analyzedSha":"3f483e80e326cc646b54b83d564b3f0c4881b9a6","analyzedAt":"2026-08-12T13:25:04.382Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}