{"record":{"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/8e6d128e4a278059ecddecaa97400b04c8ae5fd9/packages/create-jest/src/runCreate.ts#L50-L86","documentation":"create-jest reads and JSON.parses package.json before generating the Jest config; if parsing throws (syntax error, trailing comma, unquoted key, BOM, or empty file), it wraps the failure in MalformedPackageJsonError naming the offending path. The init cannot proceed because it needs to inspect `scripts.test` and the `jest`/`type` fields and may rewrite the file.","triggerScenarios":"`jest --init` while package.json contains invalid JSON: trailing commas, single-quoted strings, comments, a stray character, or a zero-byte file. The try/catch around JSON.parse(readFileSync(...)) catches any throw and rethrows this error.","commonSituations":"Hand-edited package.json with a JSON5 feature (comments/trailing commas) not valid in strict JSON; a botched merge/rebase leaving a conflict marker or duplicate key; a file accidentally overwritten or truncated; copy-pasting config snippets with unquoted keys.","solutions":["Run `node -e \"JSON.parse(require('fs').readFileSync('package.json','utf8'))\"` or `npx jsonlint package.json` to locate the exact syntax error and fix it.","Re-run `jest --init` after the JSON parses cleanly.","If the file was truncated, restore it from version control (`git checkout -- package.json`) and re-apply intended edits carefully."],"exampleFix":"// before (package.json)\n{\n  \"name\": \"app\",\n  \"scripts\": { \"test\": \"jest\", },   // trailing comma\n}\n\n// after\n{\n  \"name\": \"app\",\n  \"scripts\": { \"test\": \"jest\" }\n}","handlingStrategy":"validation","validationCode":"// Validate package.json parses before invoking jest --init.\nimport * as fs from 'node:fs';\n\nfunction validatePackageJson(filePath) {\n  try {\n    JSON.parse(fs.readFileSync(filePath, 'utf8'));\n  } catch (e) {\n    throw new Error(`${filePath} is not valid JSON: ${e.message}`);\n  }\n}\n\n// Or lint from the CLI: npx jsonlint package.json","typeGuard":"function isValidPackageJson(raw: string): boolean {\n  try { JSON.parse(raw); return true; } catch { return false; }\n}","tryCatchPattern":null,"preventionTips":["Lint package.json (e.g. `npx jsonlint package.json`) after manual edits.","Avoid JSON5 features (comments, trailing commas) in package.json.","Configure your editor to flag invalid JSON and to format on save.","Add a CI step that JSON.parses package.json to catch corruption early."],"tags":["create-jest","package-json","json-syntax","initialization"],"backgroundTag":null,"analyzedSha":"8e6d128e4a278059ecddecaa97400b04c8ae5fd9","analyzedAt":"2026-08-10T18:11:27.960Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}