{"record":{"id":"ba26d3aa11c0973d","repo":"mochajs/mocha","slug":"this-script-is-intended-to-be-run-as-a-worker-by","errorCode":null,"errorMessage":"This script is intended to be run as a worker (by the `workerpool` package).","messagePattern":"This script is intended to be run as a worker \\(by the `workerpool` package\\)\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"lib/nodejs/worker.cjs","lineNumber":33,"sourceCode":"  createInvalidArgumentTypeError,\n  createInvalidArgumentValueError,\n} = require(\"../errors.js\");\nconst workerpool = require(\"workerpool\");\nconst Mocha = require(\"../mocha.cjs\");\nconst {\n  handleRequires,\n  validateLegacyPlugin,\n} = require(\"../cli/run-helpers.cjs\");\nconst d = require(\"debug\");\nconst debug = d.debug(`mocha:parallel:worker:${process.pid}`);\nconst isDebugEnabled = d.enabled(`mocha:parallel:worker:${process.pid}`);\nconst { serialize } = require(\"./serializer.js\");\nconst { setInterval, clearInterval } = global;\n\nlet rootHooks;\n\nif (workerpool.isMainThread) {\n  throw new Error(\n    \"This script is intended to be run as a worker (by the `workerpool` package).\",\n  );\n}\n\n/**\n * Initializes some stuff on the first call to {@link run}.\n *\n * Handles `--require` and `--ui`.  Does _not_ handle `--reporter`,\n * as only the `Buffered` reporter is used.\n *\n * **This function only runs once per worker**; it overwrites itself with a no-op\n * before returning.\n *\n * @param {MochaOptions} argv - Command-line options\n */\nlet bootstrap = async (argv) => {\n  // globalSetup and globalTeardown do not run in workers\n  const plugins = await handleRequires(argv.require, {","sourceCodeStart":15,"sourceCodeEnd":51,"githubUrl":"https://github.com/mochajs/mocha/blob/6bcbee4fd9a95351cedf0fdcb14c7d696486bc5a/lib/nodejs/worker.cjs#L15-L51","documentation":"Mocha's parallel-mode worker script (lib/nodejs/worker.cjs) is designed to be loaded by the `workerpool` package inside a worker thread, not executed directly. At module load it checks `workerpool.isMainThread`; if the script is running on the main thread it means it was required or executed outside of a workerpool worker, so it throws immediately to prevent the worker bootstrap logic (root hooks, run loop) from running in the wrong context.","triggerScenarios":"Running `node lib/nodejs/worker.cjs` directly; `require()`-ing worker.cjs from user code or a test on the main thread; launching the parallel runner with a custom/incorrect worker path that resolves to this file in the main thread.","commonSituations":"Users point `--parallel` at the wrong file or write custom runners that spawn mocha's worker themselves; debugging sessions where a developer opens/executes the worker file directly; bundlers or module loaders accidentally pulling worker.cjs into the main bundle.","solutions":["Run mocha normally with `--parallel` and let mocha/workerpool spawn the worker internally","If building a custom runner, load the worker via workerpool (e.g. `workerpool.worker(...)`) inside a worker thread, not on the main thread","Remove any direct `require('mocha/lib/nodejs/worker.cjs')` from application code","Use the public Mocha programmatic API or CLI instead of internal worker modules"],"exampleFix":"// before\nconst worker = require('mocha/lib/nodejs/worker.cjs');\n// after\nconst Mocha = require('mocha');\nconst mocha = new Mocha({ parallel: true });\nmocha.run();","handlingStrategy":"validation","validationCode":"if (workerpool.isMainThread) {\n  // do not load the worker module on the main thread\n} else {\n  require('mocha/lib/nodejs/worker.cjs');\n}","typeGuard":"const isWorkerThread = typeof workerpool !== 'undefined' && !workerpool.isMainThread;","tryCatchPattern":"try {\n  require('mocha/lib/nodejs/worker.cjs');\n} catch (err) {\n  if (!/intended to be run as a worker/.test(err.message)) throw err;\n  console.error('worker.cjs must be spawned by workerpool, not required directly');\n}","preventionTips":["Never import mocha internal worker modules from user code","Use the public Mocha API or CLI for parallel runs","Guard any internal use with workerpool.isMainThread before loading"],"tags":["parallel-mode","worker-threads","misuse"],"backgroundTag":"module-run-in-wrong-context","analyzedSha":"6bcbee4fd9a95351cedf0fdcb14c7d696486bc5a","analyzedAt":"2026-09-01T03:41:47.182Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}