{"id":"cbc3984422c1e85f","repo":"jestjs/jest","slug":"there-is-malformed-json-in-packagejsonpath","errorCode":null,"errorMessage":"There is malformed json in ${packageJsonPath}","messagePattern":"There is malformed json in (.+?)","errorType":"exception","errorClass":"MalformedPackageJsonError","httpStatus":null,"severity":"error","filePath":"packages/create-jest/src/runCreate.ts","lineNumber":68,"sourceCode":"export async function runCreate(rootDir = process.cwd()): Promise<void> {\n  rootDir = tryRealpath(rootDir);\n  // prerequisite checks\n  const projectPackageJsonPath = path.join(rootDir, PACKAGE_JSON);\n\n  if (!fs.existsSync(projectPackageJsonPath)) {\n    throw new NotFoundPackageJsonError(rootDir);\n  }\n\n  const questions = [...defaultQuestions];\n  let hasJestProperty = false;\n  let projectPackageJson: ProjectPackageJson;\n\n  try {\n    projectPackageJson = JSON.parse(\n      fs.readFileSync(projectPackageJsonPath, 'utf8'),\n    ) as ProjectPackageJson;\n  } catch {\n    throw new MalformedPackageJsonError(projectPackageJsonPath);\n  }\n\n  if (projectPackageJson.jest) {\n    hasJestProperty = true;\n  }\n\n  const existingJestConfigExt = JEST_CONFIG_EXT_ORDER.find(ext =>\n    fs.existsSync(path.join(rootDir, getConfigFilename(ext))),\n  );\n\n  if (hasJestProperty || existingJestConfigExt != null) {\n    const result: {continue: boolean} = await prompts({\n      initial: true,\n      message:\n        'It seems that you already have a jest configuration, do you want to override it?',\n      name: 'continue',\n      type: 'confirm',\n    });","sourceCodeStart":50,"sourceCodeEnd":86,"githubUrl":"https://github.com/jestjs/jest/blob/f49721c78e195558b40913977c9230f5b7f559d8/packages/create-jest/src/runCreate.ts#L50-L86","documentation":"After confirming package.json exists, runCreate reads and JSON.parses it; any parse failure is caught and rethrown as MalformedPackageJsonError. create-jest needs a real JS object to read/write scripts and the `jest`/`type` fields, so it refuses to proceed on invalid JSON rather than silently corrupting the file on write.","triggerScenarios":"package.json contains a JSON syntax error: trailing comma, single-quoted strings, unquoted keys, a // or /* */ comment, a stray BOM/character, or an unclosed brace. Any of these makes JSON.parse throw and runCreate rethrows MalformedPackageJsonError.","commonSituations":"Hand-editing package.json and leaving a trailing comma; JSON5-style config that Node tolerates via require() but raw JSON.parse does not; merge-conflict markers left in the file; IDE auto-formatting disabled.","solutions":["Run a JSON validator on the file: `node -e \"JSON.parse(require('fs').readFileSync('package.json','utf8'))\"` to surface the exact parse error with a line/column.","Open package.json and fix the flagged syntax (remove trailing commas, quote keys, remove comments).","Use an IDE JSON language server or `npx fixpack` to normalize the file, then re-run create-jest.","If a git merge left conflict markers (`<<<<<<<`), resolve the merge first."],"exampleFix":"// before — package.json with trailing comma\n{\n  \"name\": \"app\",\n  \"version\": \"1.0.0\",\n}\n\n// after\n{\n  \"name\": \"app\",\n  \"version\": \"1.0.0\"\n}","handlingStrategy":"validation","validationCode":"const fs = require('node:fs');\nconst text = fs.readFileSync('package.json', 'utf8');\ntry {\n  JSON.parse(text);\n} catch (e) {\n  console.error('package.json is invalid JSON:', e.message);\n  process.exit(1);\n}\n// safe to invoke create-jest","typeGuard":"function isValidPackageJson(text: string): boolean {\n  try { JSON.parse(text); return true; } catch { return false; }\n}","tryCatchPattern":"try {\n  await runCreate(rootDir);\n} catch (e) {\n  if (e instanceof MalformedPackageJsonError) {\n    // point the user at the JSON parse error (re-parse to get line/col)\n  }\n  throw e;\n}","preventionTips":["Enable JSON schema validation in your IDE for package.json.","Never hand-add trailing commas or comments to package.json.","Run a JSON.parse CI lint over package.json before any tooling."],"tags":["create-jest","package-json","json","configuration"],"analyzedSha":"f49721c78e195558b40913977c9230f5b7f559d8","analyzedAt":"2026-08-03T20:16:28.571Z","schemaVersion":2}