jestjs/jest · error · MalformedPackageJsonError
There is malformed json in ${packageJsonPath}
Error message
There is malformed json in ${packageJsonPath} What it means
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.
Source
Thrown at packages/create-jest/src/runCreate.ts:68
export async function runCreate(rootDir = process.cwd()): Promise<void> {
rootDir = tryRealpath(rootDir);
// prerequisite checks
const projectPackageJsonPath = path.join(rootDir, PACKAGE_JSON);
if (!fs.existsSync(projectPackageJsonPath)) {
throw new NotFoundPackageJsonError(rootDir);
}
const questions = [...defaultQuestions];
let hasJestProperty = false;
let projectPackageJson: ProjectPackageJson;
try {
projectPackageJson = JSON.parse(
fs.readFileSync(projectPackageJsonPath, 'utf8'),
) as ProjectPackageJson;
} catch {
throw new MalformedPackageJsonError(projectPackageJsonPath);
}
if (projectPackageJson.jest) {
hasJestProperty = true;
}
const existingJestConfigExt = JEST_CONFIG_EXT_ORDER.find(ext =>
fs.existsSync(path.join(rootDir, getConfigFilename(ext))),
);
if (hasJestProperty || existingJestConfigExt != null) {
const result: {continue: boolean} = await prompts({
initial: true,
message:
'It seems that you already have a jest configuration, do you want to override it?',
name: 'continue',
type: 'confirm',
});View on GitHub (pinned to f49721c78e)
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.
Example fix
// before — package.json with trailing comma
{
"name": "app",
"version": "1.0.0",
}
// after
{
"name": "app",
"version": "1.0.0"
} Defensive patterns
Strategy: validation
Validate before calling
const fs = require('node:fs');
const text = fs.readFileSync('package.json', 'utf8');
try {
JSON.parse(text);
} catch (e) {
console.error('package.json is invalid JSON:', e.message);
process.exit(1);
}
// safe to invoke create-jest Type guard
function isValidPackageJson(text: string): boolean {
try { JSON.parse(text); return true; } catch { return false; }
} Try / catch
try {
await runCreate(rootDir);
} catch (e) {
if (e instanceof MalformedPackageJsonError) {
// point the user at the JSON parse error (re-parse to get line/col)
}
throw e;
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Could not find a "package.json" file in ${rootDir}
- Cannot parse ${filePath} as JSON: ${error.message}
- babel-jest: Babel ignores ${slash(path.relative(cwd, filenam
- received value must be a mock or spy function
- received value must be a mock function
AI-assisted analysis of jestjs/jest@f49721c78e (2026-08-03).
Data as JSON: /data/errors/cbc3984422c1e85f.json.
Report an issue: GitHub.