jestjs/jest · error · NotFoundPackageJsonError
Could not find a "package.json" file in ${rootDir}
Error message
Could not find a "package.json" file in ${rootDir} What it means
create-jest (the engine behind `jest --init`) calls runCreate(rootDir), resolves projectPackageJsonPath, and checks fs.existsSync. If package.json is missing it throws NotFoundPackageJsonError. Jest's initializer assumes a Node project already exists because it wants to add a `test` script and optionally a `jest` property to package.json, so a missing manifest is a hard stop rather than a prompt.
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 f49721c78e)
Solutions
- Run `npm init -y` (or `yarn init -y`) in the target directory first, then re-run create-jest.
- Run create-jest from the directory that already contains package.json, or pass that directory as the rootDir argument.
- If you intentionally have no package.json, create a minimal one with `{ "name": "..." }` before initializing.
Example fix
# before mkdir myapp && cd myapp && npx create-jest # fails: no package.json # after cd myapp && npm init -y && npx create-jest
Defensive patterns
Strategy: validation
Validate before calling
const fs = require('node:fs');
const path = require('node:path');
const rootDir = process.argv[2] ?? process.cwd();
if (!fs.existsSync(path.join(rootDir, 'package.json'))) {
console.error('No package.json in', rootDir, '- run `npm init -y` first.');
process.exit(1);
}
// safe to call runCreate now Type guard
function hasPackageJson(rootDir: string): boolean {
return fs.existsSync(path.join(rootDir, 'package.json'));
} Try / catch
try {
await runCreate(rootDir);
} catch (e) {
if (e instanceof NotFoundPackageJsonError) {
// create package.json then retry, or prompt the user
}
throw e;
} Prevention
- Always run create-jest from a directory that has package.json.
- In scaffolding scripts, assert package.json presence before invoking create-jest.
- Run `npm init -y` as part of project bootstrap templates.
When it happens
Trigger: Running `npx create-jest` or `jest --init` inside an empty/scratch directory, a directory above any package, or passing a rootDir argument that points somewhere without package.json (e.g. a subdir like ./src).
Common situations: Trying to bootstrap Jest in a fresh sandbox before `npm init`; running the initializer from the wrong cwd in a monorepo (root vs. package); CI containers that clone only a subfolder.
Related errors
- There is malformed json in ${packageJsonPath}
- 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
- Configuration in ${packageJson} is not valid. Jest expects t
AI-assisted analysis of jestjs/jest@f49721c78e (2026-08-03).
Data as JSON: /data/errors/0a17f29a723b20f6.json.
Report an issue: GitHub.