mochajs/mocha · error · Error
ERR_MOCHA_UNPARSABLE_FILE
ERR_MOCHA_UNPARSABLE_FILE
Error message
Unable to read/parse ${filepath}: ${err} What it means
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.
Source
Thrown at lib/cli/config.cjs:84
* @param {string} filepath - Config file path to load
* @returns {Object} Parsed config object
*/
exports.loadConfig = (filepath) => {
let config;
debug("loadConfig: trying to parse config at %s", filepath);
const ext = path.extname(filepath);
try {
if (ext === ".yml" || ext === ".yaml") {
config = parsers.yaml(filepath);
} else if (ext === ".js" || ext === ".cjs" || ext === ".mjs") {
const parsedConfig = parsers.js(filepath);
config = parsedConfig.default ?? parsedConfig;
} else {
config = parsers.json(filepath);
}
} catch (err) {
throw createUnparsableFileError(
`Unable to read/parse ${filepath}: ${err}`,
filepath,
);
}
return config;
};
/**
* Find ("find up") config file starting at `cwd`
*
* @param {string} [cwd] - Current working directory
* @returns {string|null} Filepath to config, if found
*/
exports.findConfig = (cwd = utils.cwd()) => {
const filepath = findUp.sync(exports.CONFIG_FILES, { cwd });
if (filepath) {
debug("findConfig: found config file %s", filepath);
}View on GitHub (pinned to 6bcbee4fd9)
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.
Example fix
// before (.mocharc.js)
module.exports = {spec: 'test/*.spec.js',,}; // syntax error
// after
module.exports = {spec: 'test/*.spec.js'}; Defensive patterns
Strategy: validation
Validate before calling
const configPath = './.mocharc.js';
try {
const cfg = require(require('path').resolve(configPath));
console.log('config OK', Object.keys(cfg));
} catch (err) {
console.error(`Invalid mocharc: ${err.message}`); // fix before running mocha
} Type guard
function isLoadableConfig(p) {
try { require(require('path').resolve(p)); return true; } catch { return false; }
} Try / catch
try {
execSync('npx mocha', {stdio: 'inherit'});
} catch (err) {
if (String(err.stderr).includes('ERR_MOCHA_UNPARSABLE_FILE')) {
console.error('Fix the mocharc file cited in the error output');
}
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- ERR_MOCHA_UNPARSABLE_FILE
- Not enough non-option arguments: got 0, need at least 1
- Not enough arguments following: ${name}
- Warning: ${warning.message}
- ERR_MOCHA_NO_FILES_MATCH_PATTERN
AI-assisted analysis of mochajs/mocha@6bcbee4fd9 (2026-09-01).
Data as JSON: /api/errors/a2dc6d7a0407499f.
Report an issue: GitHub.