{"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/f49721c78e195558b40913977c9230f5b7f559d8/packages/create-jest/src/runCreate.ts#L38-L74","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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."],"exampleFix":"# before\nmkdir myapp && cd myapp && npx create-jest   # fails: no package.json\n\n# after\ncd myapp && npm init -y && npx create-jest","handlingStrategy":"validation","validationCode":"const fs = require('node:fs');\nconst path = require('node:path');\nconst rootDir = process.argv[2] ?? process.cwd();\nif (!fs.existsSync(path.join(rootDir, 'package.json'))) {\n  console.error('No package.json in', rootDir, '- run `npm init -y` first.');\n  process.exit(1);\n}\n// safe to call runCreate now","typeGuard":"function hasPackageJson(rootDir: string): boolean {\n  return fs.existsSync(path.join(rootDir, 'package.json'));\n}","tryCatchPattern":"try {\n  await runCreate(rootDir);\n} catch (e) {\n  if (e instanceof NotFoundPackageJsonError) {\n    // create package.json then retry, or prompt the user\n  }\n  throw e;\n}","preventionTips":["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."],"tags":["create-jest","package-json","initialization","configuration"],"analyzedSha":"f49721c78e195558b40913977c9230f5b7f559d8","analyzedAt":"2026-08-03T20:16:28.571Z","schemaVersion":2}