{"record":{"id":"8621b47e52d80ccc","repo":"mochajs/mocha","slug":"err-mocha-invalid-reporter","errorCode":"ERR_MOCHA_INVALID_REPORTER","errorMessage":"${err.message}","messagePattern":"\\$\\{err\\.message\\}","errorType":"exception","errorClass":"InvalidReporterError","httpStatus":null,"severity":"error","filePath":"lib/mocha.cjs","lineNumber":328,"sourceCode":"Mocha.prototype.reporter = function (reporterName, reporterOptions) {\n  if (typeof reporterName === \"function\") {\n    this._reporter = reporterName;\n  } else {\n    reporterName = reporterName || \"spec\";\n    var reporter;\n    // Try to load a built-in reporter.\n    if (builtinReporters[reporterName]) {\n      reporter = builtinReporters[reporterName];\n    }\n    // Try to load reporters from process.cwd() and node_modules\n    if (!reporter) {\n      let foundReporter;\n      try {\n        foundReporter = require.resolve(reporterName);\n        reporter = require(foundReporter);\n      } catch (err) {\n        if (foundReporter) {\n          throw createInvalidReporterError(err.message, foundReporter);\n        }\n        // Try to load reporters from a cwd-relative path\n        try {\n          reporter = require(path.resolve(reporterName));\n        } catch (err) {\n          throw createInvalidReporterError(err.message, reporterName);\n        }\n      }\n    }\n    if (reporter.default) {\n      reporter = reporter.default;\n    }\n\n    this._reporter = reporter;\n  }\n  this.options.reporterOption = reporterOptions;\n  // alias option name is used in built-in reporters xunit/tap/progress\n  this.options.reporterOptions = reporterOptions;","sourceCodeStart":310,"sourceCodeEnd":346,"githubUrl":"https://github.com/mochajs/mocha/blob/6bcbee4fd9a95351cedf0fdcb14c7d696486bc5a/lib/mocha.cjs#L310-L346","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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)"],"exampleFix":"// before (reporter.js)\nconst missing = require('not-installed-pkg');\nmodule.exports = function () {};\n// after\nmodule.exports = function (runner, options) { /* ... */ };","handlingStrategy":"try-catch","validationCode":"try {\n  require(reporterPath);\n  console.log('reporter loads OK');\n} catch (err) {\n  console.error('reporter fails to load:', err.message);\n}","typeGuard":null,"tryCatchPattern":"try {\n  new Mocha({ reporter: myReporter });\n} catch (err) {\n  if (err.code === 'ERR_MOCHA_INVALID_REPORTER') {\n    console.error(`Reporter module error: ${err.message}`);\n  } else throw err;\n}","preventionTips":["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"],"tags":["reporter","module-loading"],"backgroundTag":"reporter-load-failed","analyzedSha":"6bcbee4fd9a95351cedf0fdcb14c7d696486bc5a","analyzedAt":"2026-09-01T03:41:47.182Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}