jestjs/jest · error · NotFoundPackageJsonError
Could not find a "package.json" file in
Error message
Could not find a "package.json" file in ${rootDir} What it means
create-jest (the engine behind `jest --init` / `npm init jest`) requires a package.json in the target directory because it writes a Jest config alongside it and optionally modifies the `scripts.test` entry. NotFoundPackageJsonError is thrown from runCreate when path.join(rootDir, 'package.json') does not exist on disk. Without a package.json there is nowhere to record the test script and no project root to anchor the generated config.
Solutions
- Run `npm init -y` (or `yarn init`) in the target directory to create a package.json, then re-run `jest --init`.
- Verify the path you passed to create-jest / `jest --init` is the intended project root and exists.
- If you already have a package.json in a parent directory, run the command from that directory instead.
Example fix
// before $ jest --init # -> Could not find a "package.json" file in /tmp/foo // after $ npm init -y && jest --init
Defensive patterns
Strategy: validation
Validate before calling
// Before calling runCreate / jest --init, verify package.json exists.
import * as path from 'node:path';
import * as fs from 'node:fs';
function ensurePackageJson(rootDir) {
const p = path.join(rootDir, 'package.json');
if (!fs.existsSync(p)) {
throw new Error(`No package.json in ${rootDir}. Run 'npm init -y' first.`);
}
return p;
} Type guard
function hasPackageJson(rootDir: string): boolean {
return fs.existsSync(path.join(rootDir, 'package.json'));
} Prevention
- Always `npm init -y` before running `jest --init` in a new directory.
- Pass an explicit, verified rootDir to create-jest rather than relying on cwd.
- In scaffolding scripts, check for package.json before invoking the Jest init flow.
When it happens
Trigger: Running `npx create-jest <dir>` or `jest --init` in a directory (or a passed argv path) that has no package.json file. Also triggered when the realpath of the supplied rootDir resolves to a folder that was never `npm init`-ed.
Common situations: Initialising Jest in a scratch/empty folder before running `npm init -y`; passing a wrong path argument to create-jest; running the init from a subdirectory of a fresh clone that has not yet been bootstrapped.
Related errors
- There is malformed json in
- Cannot parse as JSON
- Attempted to display seed but seed value is undefined
- babel-jest: Babel ignores
- Crawler retry failed: Original error
AI-assisted analysis of jestjs/jest@8e6d128e4a (2026-08-10).
Data as JSON: /api/errors/0a17f29a723b20f6.
Report an issue: GitHub.
Appendix: source
Thrown at packages/create-jest/src/runCreate.ts:56
clearLine(process.stdout);
if (error instanceof Error && Boolean(error?.stack)) {
console.error(chalk.red(error.stack));
} else {
console.error(chalk.red(error));
}
exit(1);
throw error;
}
}
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;
}
View on GitHub (pinned to 8e6d128e4a)