{"record":{"id":"0c140a60c7b3abb5","repo":"mochajs/mocha","slug":"err-mocha-unsupported-0c140a","errorCode":"ERR_MOCHA_UNSUPPORTED","errorMessage":"file output not supported in browser","messagePattern":"file output not supported in browser","errorType":"error_code","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"lib/reporters/json.js","lineNumber":50,"sourceCode":"   * @public\n   * @memberof Mocha.reporters\n   * @extends Mocha.reporters.Base\n   * @param {Runner} runner - Instance triggers reporter actions.\n   * @param {Object} [options] - runner options\n   */\n  constructor(runner, options = {}) {\n    super(runner, options);\n\n    var self = this;\n    var tests = [];\n    var pending = [];\n    var failures = [];\n    var passes = [];\n    var output;\n\n    if (options.reporterOption && options.reporterOption.output) {\n      if (utils.isBrowser()) {\n        throw createUnsupportedError(\"file output not supported in browser\");\n      }\n      output = options.reporterOption.output;\n    }\n\n    runner.on(EVENT_TEST_END, function (test) {\n      tests.push(test);\n    });\n\n    runner.on(EVENT_TEST_PASS, function (test) {\n      passes.push(test);\n    });\n\n    runner.on(EVENT_TEST_FAIL, function (test) {\n      failures.push(test);\n    });\n\n    runner.on(EVENT_TEST_PENDING, function (test) {\n      pending.push(test);","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/mochajs/mocha/blob/6bcbee4fd9a95351cedf0fdcb14c7d696486bc5a/lib/reporters/json.js#L32-L68","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Remove the output reporter option when running in the browser; collect results from the reporter object or runner events instead.","Run the JSON-reporter file output only under Node (mocha CLI or node script).","In shared config, move the output option into a Node-only mocharc or conditionally strip it for browser runs.","Write results in the browser by listening to the runner's 'end' event and serializing test results yourself (e.g. posting to a server)."],"exampleFix":"// before\nmocha.run(() => {}); // mocha.jsonc contains {\"reporterOptions\": {\"output\": \"out.json\"}}\n// after\n// remove output from browser config, or in Node-only config keep:\n// {\"reporter-option\": \"output=out.json\"} with --reporter json","handlingStrategy":"validation","validationCode":"import {isBrowser} from 'mocha/lib/utils'; // or detect env yourself\nconst isBrowser = typeof window !== 'undefined';\nif (isBrowser && reporterOptions.output) {\n  delete reporterOptions.output; // file output is Node-only\n}","typeGuard":"function canWriteFiles() {\n  return typeof process !== 'undefined' && !!process.versions?.node && typeof require === 'function';\n}","tryCatchPattern":"try {\n  new Mocha({reporter: 'json', reporterOption: {output: 'out.json'}}).run(fn);\n} catch (err) {\n  if (err.code === 'ERR_MOCHA_UNSUPPORTED') {\n    // fall back to in-memory results collection\n  }\n}","preventionTips":["Keep Node-only reporter options in a Node-only mocharc","Detect environment before configuring reporters","Collect browser results via runner events, not file output","Document that output file options are Node-only in your test harness config"],"tags":["browser","reporter","unsupported-feature"],"backgroundTag":"browser-env-unsupported-api","analyzedSha":"6bcbee4fd9a95351cedf0fdcb14c7d696486bc5a","analyzedAt":"2026-09-01T03:41:47.182Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}