{"record":{"id":"0378d161710fae7c","repo":"mochajs/mocha","slug":"err-mocha-unparsable-file-0378d1","errorCode":"ERR_MOCHA_UNPARSABLE_FILE","errorMessage":"Unable to read ${filepath}: ${err}","messagePattern":"Unable to read (.+?): (.+?)","errorType":"error_code","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"lib/cli/options.cjs","lineNumber":92,"sourceCode":" * @public\n * @alias module:lib/cli.loadPkgRc\n * @returns {external:parseArgs.Arguments|void} Parsed config. Throws if unparsableF. Empty object if file not found.\n */\nconst loadPkgRc = (args = {}) => {\n  let result;\n  if (args.package === false) {\n    return result;\n  }\n  result = {};\n  const filepath = args.package || sync(mocharc.package);\n  if (filepath) {\n    let configData;\n    try {\n      configData = readFileSync(filepath, \"utf8\");\n    } catch (err) {\n      // If `args.package` was explicitly specified, throw an error\n      if (filepath == args.package) {\n        throw createUnparsableFileError(\n          `Unable to read ${filepath}: ${err}`,\n          filepath,\n        );\n      } else {\n        debug(\"failed to read default package.json at %s; ignoring\", filepath);\n        return result;\n      }\n    }\n    try {\n      const pkg = JSON.parse(configData);\n      if (pkg.mocha) {\n        debug(\"`mocha` prop of package.json parsed: %O\", pkg.mocha);\n        result = pkg.mocha;\n      } else {\n        debug(\"no config found in %s\", filepath);\n      }\n    } catch (err) {\n      // If JSON failed to parse, throw an error.","sourceCodeStart":74,"sourceCodeEnd":110,"githubUrl":"https://github.com/mochajs/mocha/blob/6bcbee4fd9a95351cedf0fdcb14c7d696486bc5a/lib/cli/options.cjs#L74-L110","documentation":"loadPkgRc reads a package.json (default ./package.json or one given via --package) to extract a `mocha` config key. If reading the explicitly-specified package file fails, Mocha throws ERR_MOCHA_UNPARSABLE_FILE. A missing default ./package.json is silently ignored; only an explicitly requested file that cannot be read raises this error.","triggerScenarios":"Running `mocha --package ./config/pkg.json` (or MOCHA_OPTIONS/programmatic loadPkgRc with args.package) where that file does not exist or is unreadable (permissions, wrong path).","commonSituations":"Typo in --package path; file deleted or moved after CI config was written; running mocha from a different cwd so relative path resolves wrong; restricted CI permissions.","solutions":["Correct the --package path (verify with ls from the cwd mocha runs in).","Ensure the file is readable (check permissions, and that it exists in the container/CI workspace).","Remove --package if you actually want the default ./package.json behavior (missing default is ignored).","Use an absolute path in CI to avoid cwd drift."],"exampleFix":"// before\nnpx mocha --package ./conf/pakage.json\n// after\nnpx mocha --package ./config/package.json","handlingStrategy":"validation","validationCode":"const fs = require('fs');\nconst pkgPath = './config/package.json';\nif (!fs.existsSync(pkgPath)) {\n  throw new Error(`--package target missing: ${require('path').resolve(pkgPath)}`);\n}","typeGuard":"function isReadableFile(p) {\n  try { require('fs').accessSync(p); return require('fs').statSync(p).isFile(); } catch { return false; }\n}","tryCatchPattern":"try {\n  execSync('npx mocha --package ' + pkgPath, {stdio: 'inherit'});\n} catch (err) {\n  if (String(err.stderr).includes('Unable to read')) {\n    console.error(`Check that ${pkgPath} exists and is readable from the mocha cwd`);\n  }\n}","preventionTips":["Use absolute paths for --package in CI to avoid cwd drift","Verify the path exists after any file-restructuring refactor","Remember a missing default ./package.json is ignored; only explicit --package paths throw","Check container/workspace mounts include the package file"],"tags":["config","package-json","file-io","cli"],"backgroundTag":"config-file-unreadable","analyzedSha":"6bcbee4fd9a95351cedf0fdcb14c7d696486bc5a","analyzedAt":"2026-09-01T03:41:47.182Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}