mochajs/mocha · error · Error

ERR_MOCHA_UNSUPPORTED

ERR_MOCHA_UNSUPPORTED

Error message

file output not supported in browser

What it means

Mocha's JSON reporter can write results to a file via the --reporter-option output=... option, but file I/O is unavailable in browser builds where fs is stubbed. When the reporter detects a browser environment and an output file was requested, it throws this ERR_MOCHA_UNSUPPORTED error. It exists to fail fast instead of silently dropping the requested file output.

Source

Thrown at lib/reporters/json.js:50

   * @public
   * @memberof Mocha.reporters
   * @extends Mocha.reporters.Base
   * @param {Runner} runner - Instance triggers reporter actions.
   * @param {Object} [options] - runner options
   */
  constructor(runner, options = {}) {
    super(runner, options);

    var self = this;
    var tests = [];
    var pending = [];
    var failures = [];
    var passes = [];
    var output;

    if (options.reporterOption && options.reporterOption.output) {
      if (utils.isBrowser()) {
        throw createUnsupportedError("file output not supported in browser");
      }
      output = options.reporterOption.output;
    }

    runner.on(EVENT_TEST_END, function (test) {
      tests.push(test);
    });

    runner.on(EVENT_TEST_PASS, function (test) {
      passes.push(test);
    });

    runner.on(EVENT_TEST_FAIL, function (test) {
      failures.push(test);
    });

    runner.on(EVENT_TEST_PENDING, function (test) {
      pending.push(test);

View on GitHub (pinned to 6bcbee4fd9)

Solutions

  1. Remove the output reporter option when running in the browser; collect results from the reporter object or runner events instead.
  2. Run the JSON-reporter file output only under Node (mocha CLI or node script).
  3. In shared config, move the output option into a Node-only mocharc or conditionally strip it for browser runs.
  4. Write results in the browser by listening to the runner's 'end' event and serializing test results yourself (e.g. posting to a server).

Example fix

// before
mocha.run(() => {}); // mocha.jsonc contains {"reporterOptions": {"output": "out.json"}}
// after
// remove output from browser config, or in Node-only config keep:
// {"reporter-option": "output=out.json"} with --reporter json
Defensive patterns

Strategy: validation

Validate before calling

import {isBrowser} from 'mocha/lib/utils'; // or detect env yourself
const isBrowser = typeof window !== 'undefined';
if (isBrowser && reporterOptions.output) {
  delete reporterOptions.output; // file output is Node-only
}

Type guard

function canWriteFiles() {
  return typeof process !== 'undefined' && !!process.versions?.node && typeof require === 'function';
}

Try / catch

try {
  new Mocha({reporter: 'json', reporterOption: {output: 'out.json'}}).run(fn);
} catch (err) {
  if (err.code === 'ERR_MOCHA_UNSUPPORTED') {
    // fall back to in-memory results collection
  }
}

Prevention

When it happens

Trigger: Instantiating the JSON reporter (new JSONReporter(runner, {reporterOption: {output: 'result.json'}})) or running `mocha --reporter json --reporter-option output=result.json` in a browser bundle where utils.isBrowser() is true.

Common situations: Using mocha-web or a bundler that exposes Mocha's browser entry while passing CLI-style output options; shared config (e.g. mocharc) reused across Node and browser test runs; karma/webdriver-runner setups that inherit Node-only reporter flags.

Related errors


AI-assisted analysis of mochajs/mocha@6bcbee4fd9 (2026-09-01). Data as JSON: /api/errors/0c140a60c7b3abb5. Report an issue: GitHub.