{"record":{"id":"0a17f29a723b20f6","repo":"jestjs/jest","slug":"could-not-find-a-package-json-file-in-rootdir","errorCode":null,"errorMessage":"Could not find a \"package.json\" file in ${rootDir}","messagePattern":"Could not find a \"package\\.json\" file in (.+?)","errorType":"exception","errorClass":"NotFoundPackageJsonError","httpStatus":null,"severity":"error","filePath":"packages/create-jest/src/runCreate.ts","lineNumber":56,"sourceCode":"    clearLine(process.stdout);\n    if (error instanceof Error && Boolean(error?.stack)) {\n      console.error(chalk.red(error.stack));\n    } else {\n      console.error(chalk.red(error));\n    }\n\n    exit(1);\n    throw error;\n  }\n}\n\nexport 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","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/jestjs/jest/blob/8e6d128e4a278059ecddecaa97400b04c8ae5fd9/packages/create-jest/src/runCreate.ts#L38-L74","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\n$ jest --init\n# -> Could not find a \"package.json\" file in /tmp/foo\n\n// after\n$ npm init -y && jest --init","handlingStrategy":"validation","validationCode":"// Before calling runCreate / jest --init, verify package.json exists.\nimport * as path from 'node:path';\nimport * as fs from 'node:fs';\n\nfunction ensurePackageJson(rootDir) {\n  const p = path.join(rootDir, 'package.json');\n  if (!fs.existsSync(p)) {\n    throw new Error(`No package.json in ${rootDir}. Run 'npm init -y' first.`);\n  }\n  return p;\n}","typeGuard":"function hasPackageJson(rootDir: string): boolean {\n  return fs.existsSync(path.join(rootDir, 'package.json'));\n}","tryCatchPattern":null,"preventionTips":["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."],"tags":["create-jest","initialization","package-json","configuration"],"backgroundTag":null,"analyzedSha":"8e6d128e4a278059ecddecaa97400b04c8ae5fd9","analyzedAt":"2026-08-10T18:11:27.960Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}