{"record":{"id":"a2dc6d7a0407499f","repo":"mochajs/mocha","slug":"err-mocha-unparsable-file","errorCode":"ERR_MOCHA_UNPARSABLE_FILE","errorMessage":"Unable to read/parse ${filepath}: ${err}","messagePattern":"Unable to read/parse (.+?): (.+?)","errorType":"error_code","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"lib/cli/config.cjs","lineNumber":84,"sourceCode":" * @param {string} filepath - Config file path to load\n * @returns {Object} Parsed config object\n */\nexports.loadConfig = (filepath) => {\n  let config;\n  debug(\"loadConfig: trying to parse config at %s\", filepath);\n\n  const ext = path.extname(filepath);\n  try {\n    if (ext === \".yml\" || ext === \".yaml\") {\n      config = parsers.yaml(filepath);\n    } else if (ext === \".js\" || ext === \".cjs\" || ext === \".mjs\") {\n      const parsedConfig = parsers.js(filepath);\n      config = parsedConfig.default ?? parsedConfig;\n    } else {\n      config = parsers.json(filepath);\n    }\n  } catch (err) {\n    throw createUnparsableFileError(\n      `Unable to read/parse ${filepath}: ${err}`,\n      filepath,\n    );\n  }\n  return config;\n};\n\n/**\n * Find (\"find up\") config file starting at `cwd`\n *\n * @param {string} [cwd] - Current working directory\n * @returns {string|null} Filepath to config, if found\n */\nexports.findConfig = (cwd = utils.cwd()) => {\n  const filepath = findUp.sync(exports.CONFIG_FILES, { cwd });\n  if (filepath) {\n    debug(\"findConfig: found config file %s\", filepath);\n  }","sourceCodeStart":66,"sourceCodeEnd":102,"githubUrl":"https://github.com/mochajs/mocha/blob/6bcbee4fd9a95351cedf0fdcb14c7d696486bc5a/lib/cli/config.cjs#L66-L102","documentation":"When Mocha loads a JS/JSON config file (.mocharc.js/.mjs/cjs or .mocharc.json) it wraps parsing in try/catch; any read or parse failure is rethrown as ERR_MOCHA_UNPARSABLE_FILE with the original error embedded. This tells you exactly which config file Mocha could not load instead of leaking a low-level require/JSON error.","triggerScenarios":"Running mocha with a .mocharc.js/.mocharc.json that has a syntax error, uses unsupported syntax (e.g. ESM in .js on old Node), throws at import time, or is unreadable on disk.","commonSituations":"Hand-editing mocharc and leaving a trailing comma; committing a config referencing missing imports; a .mocharc.js that executes code failing at load; permission problems on the config file.","solutions":["Open the mocharc file cited in the message and fix the underlying syntax/import error shown after the colon.","Validate the file standalone: `node -e \"require('./.mocharc.js')\"` (or JSON.parse for json).","If using ESM syntax, rename to .mocharc.mjs or ensure your Node version supports it.","Restore the file from version control or fix file permissions."],"exampleFix":"// before (.mocharc.js)\nmodule.exports = {spec: 'test/*.spec.js',,}; // syntax error\n// after\nmodule.exports = {spec: 'test/*.spec.js'};","handlingStrategy":"validation","validationCode":"const configPath = './.mocharc.js';\ntry {\n  const cfg = require(require('path').resolve(configPath));\n  console.log('config OK', Object.keys(cfg));\n} catch (err) {\n  console.error(`Invalid mocharc: ${err.message}`); // fix before running mocha\n}","typeGuard":"function isLoadableConfig(p) {\n  try { require(require('path').resolve(p)); return true; } catch { return false; }\n}","tryCatchPattern":"try {\n  execSync('npx mocha', {stdio: 'inherit'});\n} catch (err) {\n  if (String(err.stderr).includes('ERR_MOCHA_UNPARSABLE_FILE')) {\n    console.error('Fix the mocharc file cited in the error output');\n  }\n}","preventionTips":["Validate mocharc changes by requiring the file in Node before committing","Never use JSON syntax in .mocharc.js or JS syntax in .mocharc.json","Use .mocharc.mjs for ESM syntax","Add a CI step that loads the config file standalone"],"tags":["config","parse-error","cli"],"backgroundTag":"config-file-parse-error","analyzedSha":"6bcbee4fd9a95351cedf0fdcb14c7d696486bc5a","analyzedAt":"2026-09-01T03:41:47.182Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}