{"record":{"id":"0deae075cf6d925f","repo":"parcel-bundler/parcel","slug":"expected-package-json-file-in-rootdir","errorCode":null,"errorMessage":"Expected package.json file in ${rootDir}","messagePattern":"Expected package\\.json file in (.+?)","errorType":"exception","errorClass":"ThrowableDiagnostic","httpStatus":null,"severity":"error","filePath":"packages/core/core/src/requests/TargetRequest.js","lineNumber":469,"sourceCode":"\n    let rootFileProject = toProjectPath(this.options.projectRoot, rootFile);\n\n    // Invalidate whenever a package.json file is added.\n    this.api.invalidateOnFileCreate({\n      fileName: 'package.json',\n      aboveFilePath: rootFileProject,\n    });\n\n    let pkg;\n    let pkgContents;\n    let pkgFilePath: ?FilePath;\n    let pkgDir: FilePath;\n    let pkgMap;\n    if (conf) {\n      pkg = (conf.config: PackageJSON);\n      let pkgFile = conf.files[0];\n      if (pkgFile == null) {\n        throw new ThrowableDiagnostic({\n          diagnostic: {\n            message: md`Expected package.json file in ${rootDir}`,\n            origin: '@parcel/core',\n          },\n        });\n      }\n      let _pkgFilePath = (pkgFilePath = pkgFile.filePath); // For Flow\n      pkgDir = path.dirname(_pkgFilePath);\n      pkgContents = await this.fs.readFile(_pkgFilePath, 'utf8');\n      pkgMap = parse(pkgContents, undefined, {tabWidth: 1});\n\n      let pp = toProjectPath(this.options.projectRoot, _pkgFilePath);\n      this.api.invalidateOnFileUpdate(pp);\n      this.api.invalidateOnFileDelete(pp);\n    } else {\n      pkg = {};\n      pkgDir = this.fs.cwd();\n    }","sourceCodeStart":451,"sourceCodeEnd":487,"githubUrl":"https://github.com/parcel-bundler/parcel/blob/59484858a1a0bcbb71f74088956bb437a2db6505/packages/core/core/src/requests/TargetRequest.js#L451-L487","documentation":"TargetRequest loads the nearest package.json via loadConfig. If a config object is returned (`conf` is truthy) but `conf.files[0]` is null/undefined, the loader could not record an actual package.json file on disk. This indicates the package.json resolution returned a virtual/injected config without a backing file, which Parcel cannot use for target parsing.","triggerScenarios":"Calling Parcel programmatically with a custom/overlay config that yields a parsed package.json but no file path; a virtual filesystem where loadConfig resolves config contents but fails to track the source file; a corrupted or empty package.json that loadConfig parsed from a non-file source.","commonSituations":"Using a memory FS in tests that injects package.json contents without registering a file; a third-party config plugin that synthesizes package.json; broken symlinks pointing at package.json; running Parcel from a directory where package.json exists but is unreadable or zero-length.","solutions":["Ensure a real, readable package.json exists at the project root directory passed to Parcel.","If using a custom/virtual FS, make sure the package.json file is registered in the filesystem (written with writeFile) before Parcel runs.","Check that the `projectRoot`/`rootDir` option points at a directory containing a valid package.json, not a parent or sibling.","Verify no config overlay plugin is replacing package.json resolution with a fileless result."],"exampleFix":"// before — virtual FS missing the file\nawait parcelFS.writeFile('/proj/index', '');\n// loadConfig finds contents but conf.files is []\n\n// after — register the real file\nawait parcelFS.writeFile('/proj/package.json', JSON.stringify({\n  name: 'app', source: 'src/index.html'\n}));","handlingStrategy":"validation","validationCode":"import { existsSync, statSync } from 'fs';\nimport path from 'path';\nfunction assertPkgJson(rootDir) {\n  const p = path.join(rootDir, 'package.json');\n  if (!existsSync(p) || !statSync(p).isFile()) {\n    throw new Error(`Missing package.json at ${rootDir}`);\n  }\n  if (statSync(p).size === 0) {\n    throw new Error(`Empty package.json at ${p}`);\n  }\n}","typeGuard":"function hasBackingFile(conf) {\n  return conf != null && Array.isArray(conf.files) && conf.files[0] != null && typeof conf.files[0].filePath === 'string';\n}","tryCatchPattern":"try {\n  await parcel.run();\n} catch (e) {\n  if (/Expected package\\.json file in/.test(e.message)) {\n    console.error('No readable package.json found. Ensure the projectRoot has a real package.json.');\n  } else throw e;\n}","preventionTips":["Always pass a projectRoot that contains a real, non-empty package.json.","When using a virtual/memory FS, write the package.json file before constructing Parcel.","Validate config-file presence in a pre-build step."],"tags":["parcel","package-json","config","filesystem","initialization"],"backgroundTag":null,"analyzedSha":"59484858a1a0bcbb71f74088956bb437a2db6505","analyzedAt":"2026-08-13T04:06:35.925Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}