mochajs/mocha · error · InvalidReporterError
ERR_MOCHA_INVALID_REPORTER
ERR_MOCHA_INVALID_REPORTER
Error message
${err.message} What it means
When Mocha loads a reporter by name it first tries require.resolve of the reporter name. If resolution succeeds but loading the module fails (the module threw or is malformed), Mocha rethrows createInvalidReporterError wrapping the underlying err.message with the resolved path.
Source
Thrown at lib/mocha.cjs:328
Mocha.prototype.reporter = function (reporterName, reporterOptions) {
if (typeof reporterName === "function") {
this._reporter = reporterName;
} else {
reporterName = reporterName || "spec";
var reporter;
// Try to load a built-in reporter.
if (builtinReporters[reporterName]) {
reporter = builtinReporters[reporterName];
}
// Try to load reporters from process.cwd() and node_modules
if (!reporter) {
let foundReporter;
try {
foundReporter = require.resolve(reporterName);
reporter = require(foundReporter);
} catch (err) {
if (foundReporter) {
throw createInvalidReporterError(err.message, foundReporter);
}
// Try to load reporters from a cwd-relative path
try {
reporter = require(path.resolve(reporterName));
} catch (err) {
throw createInvalidReporterError(err.message, reporterName);
}
}
}
if (reporter.default) {
reporter = reporter.default;
}
this._reporter = reporter;
}
this.options.reporterOption = reporterOptions;
// alias option name is used in built-in reporters xunit/tap/progress
this.options.reporterOptions = reporterOptions;View on GitHub (pinned to 6bcbee4fd9)
Solutions
- Read the wrapped err.message to find the real failure and fix the reporter module (syntax error, missing dep)
- Run `node -e "require('<resolved-reporter-path>')"` to reproduce the load failure directly
- Install missing dependencies or rebuild the reporter (e.g., after TS transpile)
- Verify the reporter supports your module system (ESM reporters may need a different loading path in your Mocha version)
Example fix
// before (reporter.js)
const missing = require('not-installed-pkg');
module.exports = function () {};
// after
module.exports = function (runner, options) { /* ... */ }; Defensive patterns
Strategy: try-catch
Validate before calling
try {
require(reporterPath);
console.log('reporter loads OK');
} catch (err) {
console.error('reporter fails to load:', err.message);
} Try / catch
try {
new Mocha({ reporter: myReporter });
} catch (err) {
if (err.code === 'ERR_MOCHA_INVALID_REPORTER') {
console.error(`Reporter module error: ${err.message}`);
} else throw err;
} Prevention
- Smoke-test custom reporters with `node -e "require('./reporter')"` after edits
- Keep reporter module top-level code minimal and error-free
- Pin and install all reporter dependencies
- Test reporters in CI so broken modules are caught before release
When it happens
Trigger: A custom reporter that resolves (exists at the found path) but throws during require — e.g. syntax errors, missing dependencies, or top-level code crashing inside the reporter module.
Common situations: Custom reporter with a bug at module top level; reporter depending on an uninstalled package; reporter written for a different Node/module system; stale build output.
Related errors
- ERR_MOCHA_UNSUPPORTED
- ERR_MOCHA_UNSUPPORTED
- ERR_MOCHA_INVALID_REPORTER (reporter) or ERR_MOCHA_INVALID_INTERFACE (ui)
AI-assisted analysis of mochajs/mocha@6bcbee4fd9 (2026-09-01).
Data as JSON: /api/errors/8621b47e52d80ccc.
Report an issue: GitHub.