{
  "type": "module",
  "source": "doc/api/worker_threads.md",
  "modules": [
    {
      "textRaw": "Worker Threads",
      "name": "worker_threads",
      "introduced_in": "v10.5.0",
      "stability": 2,
      "stabilityText": "Stable",
      "desc": "<p>The <code>worker_threads</code> module enables the use of threads that execute JavaScript\nin parallel. To access it:</p>\n<pre><code class=\"language-js\">const worker = require('worker_threads');\n</code></pre>\n<p>Workers (threads) are useful for performing CPU-intensive JavaScript operations.\nThey will not help much with I/O-intensive work. Node.js’s built-in asynchronous\nI/O operations are more efficient than Workers can be.</p>\n<p>Unlike <code>child_process</code> or <code>cluster</code>, <code>worker_threads</code> can share memory. They do\nso by transferring <code>ArrayBuffer</code> instances or sharing <code>SharedArrayBuffer</code>\ninstances.</p>\n<pre><code class=\"language-js\">const {\n  Worker, isMainThread, parentPort, workerData\n} = require('worker_threads');\n\nif (isMainThread) {\n  module.exports = function parseJSAsync(script) {\n    return new Promise((resolve, reject) => {\n      const worker = new Worker(__filename, {\n        workerData: script\n      });\n      worker.on('message', resolve);\n      worker.on('error', reject);\n      worker.on('exit', (code) => {\n        if (code !== 0)\n          reject(new Error(`Worker stopped with exit code ${code}`));\n      });\n    });\n  };\n} else {\n  const { parse } = require('some-js-parsing-library');\n  const script = workerData;\n  parentPort.postMessage(parse(script));\n}\n</code></pre>\n<p>The above example spawns a Worker thread for each <code>parse()</code> call. In actual\npractice, use a pool of Workers instead for these kinds of tasks. Otherwise, the\noverhead of creating Workers would likely exceed their benefit.</p>\n<p>When implementing a worker pool, use the <a href=\"async_hooks.html#async_hooks_class_asyncresource\"><code>AsyncResource</code></a> API to inform\ndiagnostic tools (e.g. in order to provide asynchronous stack traces) about the\ncorrelation between tasks and their outcomes.</p>",
      "modules": [
        {
          "textRaw": "`worker.isMainThread`",
          "name": "`worker.ismainthread`",
          "meta": {
            "added": [
              "v10.5.0"
            ],
            "changes": []
          },
          "desc": "<ul>\n<li><a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type\" class=\"type\">&lt;boolean&gt;</a></li>\n</ul>\n<p>Is <code>true</code> if this code is not running inside of a <a href=\"#worker_threads_class_worker\"><code>Worker</code></a> thread.</p>\n<pre><code class=\"language-js\">const { Worker, isMainThread } = require('worker_threads');\n\nif (isMainThread) {\n  // This re-loads the current file inside a Worker instance.\n  new Worker(__filename);\n} else {\n  console.log('Inside Worker!');\n  console.log(isMainThread);  // Prints 'false'.\n}\n</code></pre>",
          "type": "module",
          "displayName": "`worker.isMainThread`"
        },
        {
          "textRaw": "`worker.moveMessagePortToContext(port, contextifiedSandbox)`",
          "name": "`worker.movemessageporttocontext(port,_contextifiedsandbox)`",
          "meta": {
            "added": [
              "v11.13.0"
            ],
            "changes": []
          },
          "desc": "<ul>\n<li>\n<p><code>port</code> <a href=\"worker_threads.html#worker_threads_class_messageport\" class=\"type\">&lt;MessagePort&gt;</a> The message port which will be transferred.</p>\n</li>\n<li>\n<p><code>contextifiedSandbox</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object\" class=\"type\">&lt;Object&gt;</a> A <a href=\"vm.html#vm_what_does_it_mean_to_contextify_an_object\">contextified</a> object as returned by the\n<code>vm.createContext()</code> method.</p>\n</li>\n<li>\n<p>Returns: <a href=\"worker_threads.html#worker_threads_class_messageport\" class=\"type\">&lt;MessagePort&gt;</a></p>\n</li>\n</ul>\n<p>Transfer a <code>MessagePort</code> to a different <a href=\"vm.html\"><code>vm</code></a> Context. The original <code>port</code>\nobject will be rendered unusable, and the returned <code>MessagePort</code> instance will\ntake its place.</p>\n<p>The returned <code>MessagePort</code> will be an object in the target context, and will\ninherit from its global <code>Object</code> class. Objects passed to the\n<a href=\"https://developer.mozilla.org/en-US/docs/Web/API/MessagePort/onmessage\"><code>port.onmessage()</code></a> listener will also be created in the target context\nand inherit from its global <code>Object</code> class.</p>\n<p>However, the created <code>MessagePort</code> will no longer inherit from\n<a href=\"events.html\"><code>EventEmitter</code></a>, and only <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/MessagePort/onmessage\"><code>port.onmessage()</code></a> can be used to receive\nevents using it.</p>",
          "type": "module",
          "displayName": "`worker.moveMessagePortToContext(port, contextifiedSandbox)`"
        },
        {
          "textRaw": "`worker.parentPort`",
          "name": "`worker.parentport`",
          "meta": {
            "added": [
              "v10.5.0"
            ],
            "changes": []
          },
          "desc": "<ul>\n<li><a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type\" class=\"type\">&lt;null&gt;</a> | <a href=\"worker_threads.html#worker_threads_class_messageport\" class=\"type\">&lt;MessagePort&gt;</a></li>\n</ul>\n<p>If this thread was spawned as a <a href=\"#worker_threads_class_worker\"><code>Worker</code></a>, this will be a <a href=\"#worker_threads_class_messageport\"><code>MessagePort</code></a>\nallowing communication with the parent thread. Messages sent using\n<code>parentPort.postMessage()</code> will be available in the parent thread\nusing <code>worker.on('message')</code>, and messages sent from the parent thread\nusing <code>worker.postMessage()</code> will be available in this thread using\n<code>parentPort.on('message')</code>.</p>\n<pre><code class=\"language-js\">const { Worker, isMainThread, parentPort } = require('worker_threads');\n\nif (isMainThread) {\n  const worker = new Worker(__filename);\n  worker.once('message', (message) => {\n    console.log(message);  // Prints 'Hello, world!'.\n  });\n  worker.postMessage('Hello, world!');\n} else {\n  // When a message from the parent thread is received, send it back:\n  parentPort.once('message', (message) => {\n    parentPort.postMessage(message);\n  });\n}\n</code></pre>",
          "type": "module",
          "displayName": "`worker.parentPort`"
        },
        {
          "textRaw": "`worker.receiveMessageOnPort(port)`",
          "name": "`worker.receivemessageonport(port)`",
          "meta": {
            "added": [
              "v12.3.0"
            ],
            "changes": []
          },
          "desc": "<ul>\n<li>\n<p><code>port</code> <a href=\"worker_threads.html#worker_threads_class_messageport\" class=\"type\">&lt;MessagePort&gt;</a></p>\n</li>\n<li>\n<p>Returns: <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object\" class=\"type\">&lt;Object&gt;</a> | <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type\" class=\"type\">&lt;undefined&gt;</a></p>\n</li>\n</ul>\n<p>Receive a single message from a given <code>MessagePort</code>. If no message is available,\n<code>undefined</code> is returned, otherwise an object with a single <code>message</code> property\nthat contains the message payload, corresponding to the oldest message in the\n<code>MessagePort</code>’s queue.</p>\n<pre><code class=\"language-js\">const { MessageChannel, receiveMessageOnPort } = require('worker_threads');\nconst { port1, port2 } = new MessageChannel();\nport1.postMessage({ hello: 'world' });\n\nconsole.log(receiveMessageOnPort(port2));\n// Prints: { message: { hello: 'world' } }\nconsole.log(receiveMessageOnPort(port2));\n// Prints: undefined\n</code></pre>\n<p>When this function is used, no <code>'message'</code> event will be emitted and the\n<code>onmessage</code> listener will not be invoked.</p>",
          "type": "module",
          "displayName": "`worker.receiveMessageOnPort(port)`"
        },
        {
          "textRaw": "`worker.resourceLimits`",
          "name": "`worker.resourcelimits`",
          "meta": {
            "added": [
              "v12.16.0"
            ],
            "changes": []
          },
          "desc": "<ul>\n<li>\n<p><a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object\" class=\"type\">&lt;Object&gt;</a></p>\n<ul>\n<li><code>maxYoungGenerationSizeMb</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type\" class=\"type\">&lt;number&gt;</a></li>\n<li><code>maxOldGenerationSizeMb</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type\" class=\"type\">&lt;number&gt;</a></li>\n<li><code>codeRangeSizeMb</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type\" class=\"type\">&lt;number&gt;</a></li>\n</ul>\n</li>\n</ul>\n<p>Provides the set of JS engine resource constraints inside this Worker thread.\nIf the <code>resourceLimits</code> option was passed to the <a href=\"#worker_threads_class_worker\"><code>Worker</code></a> constructor,\nthis matches its values.</p>\n<p>If this is used in the main thread, its value is an empty object.</p>",
          "type": "module",
          "displayName": "`worker.resourceLimits`"
        },
        {
          "textRaw": "`worker.SHARE_ENV`",
          "name": "`worker.share_env`",
          "meta": {
            "added": [
              "v11.14.0"
            ],
            "changes": []
          },
          "desc": "<ul>\n<li><a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type\" class=\"type\">&lt;symbol&gt;</a></li>\n</ul>\n<p>A special value that can be passed as the <code>env</code> option of the <a href=\"#worker_threads_class_worker\"><code>Worker</code></a>\nconstructor, to indicate that the current thread and the Worker thread should\nshare read and write access to the same set of environment variables.</p>\n<pre><code class=\"language-js\">const { Worker, SHARE_ENV } = require('worker_threads');\nnew Worker('process.env.SET_IN_WORKER = \"foo\"', { eval: true, env: SHARE_ENV })\n  .on('exit', () => {\n    console.log(process.env.SET_IN_WORKER);  // Prints 'foo'.\n  });\n</code></pre>",
          "type": "module",
          "displayName": "`worker.SHARE_ENV`"
        },
        {
          "textRaw": "`worker.threadId`",
          "name": "`worker.threadid`",
          "meta": {
            "added": [
              "v10.5.0"
            ],
            "changes": []
          },
          "desc": "<ul>\n<li><a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type\" class=\"type\">&lt;integer&gt;</a></li>\n</ul>\n<p>An integer identifier for the current thread. On the corresponding worker object\n(if there is any), it is available as <a href=\"#worker_threads_worker_threadid_1\"><code>worker.threadId</code></a>.\nThis value is unique for each <a href=\"#worker_threads_class_worker\"><code>Worker</code></a> instance inside a single process.</p>",
          "type": "module",
          "displayName": "`worker.threadId`"
        },
        {
          "textRaw": "`worker.workerData`",
          "name": "`worker.workerdata`",
          "meta": {
            "added": [
              "v10.5.0"
            ],
            "changes": []
          },
          "desc": "<p>An arbitrary JavaScript value that contains a clone of the data passed\nto this thread’s <code>Worker</code> constructor.</p>\n<p>The data is cloned as if using <a href=\"#worker_threads_port_postmessage_value_transferlist\"><code>postMessage()</code></a>,\naccording to the <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm\">HTML structured clone algorithm</a>.</p>\n<pre><code class=\"language-js\">const { Worker, isMainThread, workerData } = require('worker_threads');\n\nif (isMainThread) {\n  const worker = new Worker(__filename, { workerData: 'Hello, world!' });\n} else {\n  console.log(workerData);  // Prints 'Hello, world!'.\n}\n</code></pre>",
          "type": "module",
          "displayName": "`worker.workerData`"
        },
        {
          "textRaw": "Class: `MessageChannel`",
          "name": "class:_`messagechannel`",
          "meta": {
            "added": [
              "v10.5.0"
            ],
            "changes": []
          },
          "desc": "<p>Instances of the <code>worker.MessageChannel</code> class represent an asynchronous,\ntwo-way communications channel.\nThe <code>MessageChannel</code> has no methods of its own. <code>new MessageChannel()</code>\nyields an object with <code>port1</code> and <code>port2</code> properties, which refer to linked\n<a href=\"#worker_threads_class_messageport\"><code>MessagePort</code></a> instances.</p>\n<pre><code class=\"language-js\">const { MessageChannel } = require('worker_threads');\n\nconst { port1, port2 } = new MessageChannel();\nport1.on('message', (message) => console.log('received', message));\nport2.postMessage({ foo: 'bar' });\n// Prints: received { foo: 'bar' } from the `port1.on('message')` listener\n</code></pre>",
          "type": "module",
          "displayName": "Class: `MessageChannel`"
        },
        {
          "textRaw": "Class: `MessagePort`",
          "name": "class:_`messageport`",
          "meta": {
            "added": [
              "v10.5.0"
            ],
            "changes": []
          },
          "desc": "<ul>\n<li>Extends: <a href=\"events.html#events_class_eventemitter\" class=\"type\">&lt;EventEmitter&gt;</a></li>\n</ul>\n<p>Instances of the <code>worker.MessagePort</code> class represent one end of an\nasynchronous, two-way communications channel. It can be used to transfer\nstructured data, memory regions and other <code>MessagePort</code>s between different\n<a href=\"#worker_threads_class_worker\"><code>Worker</code></a>s.</p>\n<p>With the exception of <code>MessagePort</code>s being <a href=\"events.html\"><code>EventEmitter</code></a>s rather\nthan <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/EventTarget\"><code>EventTarget</code></a>s, this implementation matches <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/MessagePort\">browser <code>MessagePort</code></a>s.</p>",
          "modules": [
            {
              "textRaw": "Event: `'close'`",
              "name": "event:_`'close'`",
              "meta": {
                "added": [
                  "v10.5.0"
                ],
                "changes": []
              },
              "desc": "<p>The <code>'close'</code> event is emitted once either side of the channel has been\ndisconnected.</p>\n<pre><code class=\"language-js\">const { MessageChannel } = require('worker_threads');\nconst { port1, port2 } = new MessageChannel();\n\n// Prints:\n//   foobar\n//   closed!\nport2.on('message', (message) => console.log(message));\nport2.on('close', () => console.log('closed!'));\n\nport1.postMessage('foobar');\nport1.close();\n</code></pre>",
              "type": "module",
              "displayName": "Event: `'close'`"
            },
            {
              "textRaw": "Event: `'message'`",
              "name": "event:_`'message'`",
              "meta": {
                "added": [
                  "v10.5.0"
                ],
                "changes": []
              },
              "desc": "<ul>\n<li><code>value</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types\" class=\"type\">&lt;any&gt;</a> The transmitted value</li>\n</ul>\n<p>The <code>'message'</code> event is emitted for any incoming message, containing the cloned\ninput of <a href=\"#worker_threads_port_postmessage_value_transferlist\"><code>port.postMessage()</code></a>.</p>\n<p>Listeners on this event will receive a clone of the <code>value</code> parameter as passed\nto <code>postMessage()</code> and no further arguments.</p>",
              "type": "module",
              "displayName": "Event: `'message'`"
            },
            {
              "textRaw": "`port.close()`",
              "name": "`port.close()`",
              "meta": {
                "added": [
                  "v10.5.0"
                ],
                "changes": []
              },
              "desc": "<p>Disables further sending of messages on either side of the connection.\nThis method can be called when no further communication will happen over this\n<code>MessagePort</code>.</p>\n<p>The <a href=\"#worker_threads_event_close\"><code>'close'</code> event</a> will be emitted on both <code>MessagePort</code> instances that\nare part of the channel.</p>",
              "type": "module",
              "displayName": "`port.close()`"
            },
            {
              "textRaw": "`port.postMessage(value[, transferList])`",
              "name": "`port.postmessage(value[,_transferlist])`",
              "meta": {
                "added": [
                  "v10.5.0"
                ],
                "changes": []
              },
              "desc": "<ul>\n<li><code>value</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types\" class=\"type\">&lt;any&gt;</a></li>\n<li><code>transferList</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object\" class=\"type\">&lt;Object[]&gt;</a></li>\n</ul>\n<p>Sends a JavaScript value to the receiving side of this channel.\n<code>value</code> will be transferred in a way which is compatible with\nthe <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm\">HTML structured clone algorithm</a>.</p>\n<p>In particular, the significant differences to <code>JSON</code> are:</p>\n<ul>\n<li><code>value</code> may contain circular references.</li>\n<li><code>value</code> may contain instances of builtin JS types such as <code>RegExp</code>s,\n<code>BigInt</code>s, <code>Map</code>s, <code>Set</code>s, etc.</li>\n<li><code>value</code> may contain typed arrays, both using <code>ArrayBuffer</code>s\nand <code>SharedArrayBuffer</code>s.</li>\n<li><code>value</code> may contain <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Module\"><code>WebAssembly.Module</code></a> instances.</li>\n<li><code>value</code> may not contain native (C++-backed) objects other than <code>MessagePort</code>s.</li>\n</ul>\n<pre><code class=\"language-js\">const { MessageChannel } = require('worker_threads');\nconst { port1, port2 } = new MessageChannel();\n\nport1.on('message', (message) => console.log(message));\n\nconst circularData = {};\ncircularData.foo = circularData;\n// Prints: { foo: [Circular] }\nport2.postMessage(circularData);\n</code></pre>\n<p><code>transferList</code> may be a list of <code>ArrayBuffer</code> and <code>MessagePort</code> objects.\nAfter transferring, they will not be usable on the sending side of the channel\nanymore (even if they are not contained in <code>value</code>). Unlike with\n<a href=\"child_process.html\">child processes</a>, transferring handles such as network sockets is currently\nnot supported.</p>\n<p>If <code>value</code> contains <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer\"><code>SharedArrayBuffer</code></a> instances, those will be accessible\nfrom either thread. They cannot be listed in <code>transferList</code>.</p>\n<p><code>value</code> may still contain <code>ArrayBuffer</code> instances that are not in\n<code>transferList</code>; in that case, the underlying memory is copied rather than moved.</p>\n<pre><code class=\"language-js\">const { MessageChannel } = require('worker_threads');\nconst { port1, port2 } = new MessageChannel();\n\nport1.on('message', (message) => console.log(message));\n\nconst uint8Array = new Uint8Array([ 1, 2, 3, 4 ]);\n// This posts a copy of `uint8Array`:\nport2.postMessage(uint8Array);\n// This does not copy data, but renders `uint8Array` unusable:\nport2.postMessage(uint8Array, [ uint8Array.buffer ]);\n\n// The memory for the `sharedUint8Array` will be accessible from both the\n// original and the copy received by `.on('message')`:\nconst sharedUint8Array = new Uint8Array(new SharedArrayBuffer(4));\nport2.postMessage(sharedUint8Array);\n\n// This transfers a freshly created message port to the receiver.\n// This can be used, for example, to create communication channels between\n// multiple `Worker` threads that are children of the same parent thread.\nconst otherChannel = new MessageChannel();\nport2.postMessage({ port: otherChannel.port1 }, [ otherChannel.port1 ]);\n</code></pre>\n<p>Because the object cloning uses the structured clone algorithm,\nnon-enumerable properties, property accessors, and object prototypes are\nnot preserved. In particular, <a href=\"buffer.html\"><code>Buffer</code></a> objects will be read as\nplain <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array\"><code>Uint8Array</code></a>s on the receiving side.</p>\n<p>The message object will be cloned immediately, and can be modified after\nposting without having side effects.</p>\n<p>For more information on the serialization and deserialization mechanisms\nbehind this API, see the <a href=\"v8.html#v8_serialization_api\">serialization API of the <code>v8</code> module</a>.</p>",
              "type": "module",
              "displayName": "`port.postMessage(value[, transferList])`"
            },
            {
              "textRaw": "`port.ref()`",
              "name": "`port.ref()`",
              "meta": {
                "added": [
                  "v10.5.0"
                ],
                "changes": []
              },
              "desc": "<p>Opposite of <code>unref()</code>. Calling <code>ref()</code> on a previously <code>unref()</code>ed port will\n<em>not</em> let the program exit if it's the only active handle left (the default\nbehavior). If the port is <code>ref()</code>ed, calling <code>ref()</code> again will have no effect.</p>\n<p>If listeners are attached or removed using <code>.on('message')</code>, the port will\nbe <code>ref()</code>ed and <code>unref()</code>ed automatically depending on whether\nlisteners for the event exist.</p>",
              "type": "module",
              "displayName": "`port.ref()`"
            },
            {
              "textRaw": "`port.start()`",
              "name": "`port.start()`",
              "meta": {
                "added": [
                  "v10.5.0"
                ],
                "changes": []
              },
              "desc": "<p>Starts receiving messages on this <code>MessagePort</code>. When using this port\nas an event emitter, this will be called automatically once <code>'message'</code>\nlisteners are attached.</p>\n<p>This method exists for parity with the Web <code>MessagePort</code> API. In Node.js,\nit is only useful for ignoring messages when no event listener is present.\nNode.js also diverges in its handling of <code>.onmessage</code>. Setting it will\nautomatically call <code>.start()</code>, but unsetting it will let messages queue up\nuntil a new handler is set or the port is discarded.</p>",
              "type": "module",
              "displayName": "`port.start()`"
            },
            {
              "textRaw": "`port.unref()`",
              "name": "`port.unref()`",
              "meta": {
                "added": [
                  "v10.5.0"
                ],
                "changes": []
              },
              "desc": "<p>Calling <code>unref()</code> on a port will allow the thread to exit if this is the only\nactive handle in the event system. If the port is already <code>unref()</code>ed calling\n<code>unref()</code> again will have no effect.</p>\n<p>If listeners are attached or removed using <code>.on('message')</code>, the port will\nbe <code>ref()</code>ed and <code>unref()</code>ed automatically depending on whether\nlisteners for the event exist.</p>",
              "type": "module",
              "displayName": "`port.unref()`"
            }
          ],
          "type": "module",
          "displayName": "Class: `MessagePort`"
        },
        {
          "textRaw": "Class: `Worker`",
          "name": "class:_`worker`",
          "meta": {
            "added": [
              "v10.5.0"
            ],
            "changes": []
          },
          "desc": "<ul>\n<li>Extends: <a href=\"events.html#events_class_eventemitter\" class=\"type\">&lt;EventEmitter&gt;</a></li>\n</ul>\n<p>The <code>Worker</code> class represents an independent JavaScript execution thread.\nMost Node.js APIs are available inside of it.</p>\n<p>Notable differences inside a Worker environment are:</p>\n<ul>\n<li>The <a href=\"process.html#process_process_stdin\"><code>process.stdin</code></a>, <a href=\"process.html#process_process_stdout\"><code>process.stdout</code></a> and <a href=\"process.html#process_process_stderr\"><code>process.stderr</code></a>\nmay be redirected by the parent thread.</li>\n<li>The <a href=\"#worker_threads_worker_ismainthread\"><code>require('worker_threads').isMainThread</code></a> property is set to <code>false</code>.</li>\n<li>The <a href=\"#worker_threads_worker_parentport\"><code>require('worker_threads').parentPort</code></a> message port is available.</li>\n<li><a href=\"process.html#process_process_exit_code\"><code>process.exit()</code></a> does not stop the whole program, just the single thread,\nand <a href=\"process.html#process_process_abort\"><code>process.abort()</code></a> is not available.</li>\n<li><a href=\"process.html#process_process_chdir_directory\"><code>process.chdir()</code></a> and <code>process</code> methods that set group or user ids\nare not available.</li>\n<li><a href=\"process.html#process_process_env\"><code>process.env</code></a> is a copy of the parent thread's environment variables,\nunless otherwise specified. Changes to one copy will not be visible in other\nthreads, and will not be visible to native add-ons (unless\n<a href=\"#worker_threads_worker_share_env\"><code>worker.SHARE_ENV</code></a> has been passed as the <code>env</code> option to the\n<a href=\"#worker_threads_class_worker\"><code>Worker</code></a> constructor).</li>\n<li><a href=\"process.html#process_process_title\"><code>process.title</code></a> cannot be modified.</li>\n<li>Signals will not be delivered through <a href=\"process.html#process_signal_events\"><code>process.on('...')</code></a>.</li>\n<li>Execution may stop at any point as a result of <a href=\"#worker_threads_worker_terminate\"><code>worker.terminate()</code></a>\nbeing invoked.</li>\n<li>IPC channels from parent processes are not accessible.</li>\n<li>The <a href=\"tracing.html\"><code>trace_events</code></a> module is not supported.</li>\n<li>Native add-ons can only be loaded from multiple threads if they fulfill\n<a href=\"addons.html#addons_worker_support\">certain conditions</a>.</li>\n</ul>\n<p>Creating <code>Worker</code> instances inside of other <code>Worker</code>s is possible.</p>\n<p>Like <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API\">Web Workers</a> and the <a href=\"cluster.html\"><code>cluster</code> module</a>, two-way communication can be\nachieved through inter-thread message passing. Internally, a <code>Worker</code> has a\nbuilt-in pair of <a href=\"#worker_threads_class_messageport\"><code>MessagePort</code></a>s that are already associated with each other\nwhen the <code>Worker</code> is created. While the <code>MessagePort</code> object on the parent side\nis not directly exposed, its functionalities are exposed through\n<a href=\"#worker_threads_worker_postmessage_value_transferlist\"><code>worker.postMessage()</code></a> and the <a href=\"#worker_threads_event_message_1\"><code>worker.on('message')</code></a> event\non the <code>Worker</code> object for the parent thread.</p>\n<p>To create custom messaging channels (which is encouraged over using the default\nglobal channel because it facilitates separation of concerns), users can create\na <code>MessageChannel</code> object on either thread and pass one of the\n<code>MessagePort</code>s on that <code>MessageChannel</code> to the other thread through a\npre-existing channel, such as the global one.</p>\n<p>See <a href=\"#worker_threads_port_postmessage_value_transferlist\"><code>port.postMessage()</code></a> for more information on how messages are passed,\nand what kind of JavaScript values can be successfully transported through\nthe thread barrier.</p>\n<pre><code class=\"language-js\">const assert = require('assert');\nconst {\n  Worker, MessageChannel, MessagePort, isMainThread, parentPort\n} = require('worker_threads');\nif (isMainThread) {\n  const worker = new Worker(__filename);\n  const subChannel = new MessageChannel();\n  worker.postMessage({ hereIsYourPort: subChannel.port1 }, [subChannel.port1]);\n  subChannel.port2.on('message', (value) => {\n    console.log('received:', value);\n  });\n} else {\n  parentPort.once('message', (value) => {\n    assert(value.hereIsYourPort instanceof MessagePort);\n    value.hereIsYourPort.postMessage('the worker is sending this');\n    value.hereIsYourPort.close();\n  });\n}\n</code></pre>",
          "modules": [
            {
              "textRaw": "`new Worker(filename[, options])`",
              "name": "`new_worker(filename[,_options])`",
              "meta": {
                "added": [
                  "v10.5.0"
                ],
                "changes": [
                  {
                    "version": "v12.16.0",
                    "pr-url": "https://github.com/nodejs/node/pull/26628",
                    "description": "The `resourceLimits` option was introduced."
                  },
                  {
                    "version": "v12.16.0",
                    "pr-url": "https://github.com/nodejs/node/pull/30559",
                    "description": "The `argv` option was introduced."
                  }
                ]
              },
              "desc": "<ul>\n<li><code>filename</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type\" class=\"type\">&lt;string&gt;</a> The path to the Worker’s main script. Must be\neither an absolute path or a relative path (i.e. relative to the\ncurrent working directory) starting with <code>./</code> or <code>../</code>.\nIf <code>options.eval</code> is <code>true</code>, this is a string containing JavaScript code\nrather than a path.</li>\n<li>\n<p><code>options</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object\" class=\"type\">&lt;Object&gt;</a></p>\n<ul>\n<li><code>argv</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types\" class=\"type\">&lt;any[]&gt;</a> List of arguments which would be stringified and appended to\n<code>process.argv</code> in the worker. This is mostly similar to the <code>workerData</code>\nbut the values will be available on the global <code>process.argv</code> as if they\nwere passed as CLI options to the script.</li>\n<li><code>env</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object\" class=\"type\">&lt;Object&gt;</a> If set, specifies the initial value of <code>process.env</code> inside\nthe Worker thread. As a special value, <a href=\"#worker_threads_worker_share_env\"><code>worker.SHARE_ENV</code></a> may be used\nto specify that the parent thread and the child thread should share their\nenvironment variables; in that case, changes to one thread’s <code>process.env</code>\nobject will affect the other thread as well. <strong>Default:</strong> <code>process.env</code>.</li>\n<li><code>eval</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type\" class=\"type\">&lt;boolean&gt;</a> If <code>true</code>, interpret the first argument to the constructor\nas a script that is executed once the worker is online.</li>\n<li><code>execArgv</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type\" class=\"type\">&lt;string[]&gt;</a> List of node CLI options passed to the worker.\nV8 options (such as <code>--max-old-space-size</code>) and options that affect the\nprocess (such as <code>--title</code>) are not supported. If set, this will be provided\nas <a href=\"process.html#process_process_execargv\"><code>process.execArgv</code></a> inside the worker. By default, options will be\ninherited from the parent thread.</li>\n<li><code>stdin</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type\" class=\"type\">&lt;boolean&gt;</a> If this is set to <code>true</code>, then <code>worker.stdin</code> will\nprovide a writable stream whose contents will appear as <code>process.stdin</code>\ninside the Worker. By default, no data is provided.</li>\n<li><code>stdout</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type\" class=\"type\">&lt;boolean&gt;</a> If this is set to <code>true</code>, then <code>worker.stdout</code> will\nnot automatically be piped through to <code>process.stdout</code> in the parent.</li>\n<li><code>stderr</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type\" class=\"type\">&lt;boolean&gt;</a> If this is set to <code>true</code>, then <code>worker.stderr</code> will\nnot automatically be piped through to <code>process.stderr</code> in the parent.</li>\n<li><code>workerData</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types\" class=\"type\">&lt;any&gt;</a> Any JavaScript value that will be cloned and made\navailable as <a href=\"#worker_threads_worker_workerdata\"><code>require('worker_threads').workerData</code></a>. The cloning will\noccur as described in the <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm\">HTML structured clone algorithm</a>, and an error\nwill be thrown if the object cannot be cloned (e.g. because it contains\n<code>function</code>s).</li>\n<li>\n<p><code>resourceLimits</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object\" class=\"type\">&lt;Object&gt;</a> An optional set of resource limits for the new\nJS engine instance. Reaching these limits will lead to termination of the\n<code>Worker</code> instance. These limits only affect the JS engine, and no external\ndata, including no <code>ArrayBuffer</code>s. Even if these limits are set, the process\nmay still abort if it encounters a global out-of-memory situation.</p>\n<ul>\n<li><code>maxOldGenerationSizeMb</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type\" class=\"type\">&lt;number&gt;</a> The maximum size of the main heap in MB.</li>\n<li><code>maxYoungGenerationSizeMb</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type\" class=\"type\">&lt;number&gt;</a> The maximum size of a heap space for\nrecently created objects.</li>\n<li><code>codeRangeSizeMb</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type\" class=\"type\">&lt;number&gt;</a> The size of a pre-allocated memory range\nused for generated code.</li>\n</ul>\n</li>\n</ul>\n</li>\n</ul>",
              "type": "module",
              "displayName": "`new Worker(filename[, options])`"
            },
            {
              "textRaw": "Event: `'error'`",
              "name": "event:_`'error'`",
              "meta": {
                "added": [
                  "v10.5.0"
                ],
                "changes": []
              },
              "desc": "<ul>\n<li><code>err</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error\" class=\"type\">&lt;Error&gt;</a></li>\n</ul>\n<p>The <code>'error'</code> event is emitted if the worker thread throws an uncaught\nexception. In that case, the worker will be terminated.</p>",
              "type": "module",
              "displayName": "Event: `'error'`"
            },
            {
              "textRaw": "Event: `'exit'`",
              "name": "event:_`'exit'`",
              "meta": {
                "added": [
                  "v10.5.0"
                ],
                "changes": []
              },
              "desc": "<ul>\n<li><code>exitCode</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type\" class=\"type\">&lt;integer&gt;</a></li>\n</ul>\n<p>The <code>'exit'</code> event is emitted once the worker has stopped. If the worker\nexited by calling <a href=\"process.html#process_process_exit_code\"><code>process.exit()</code></a>, the <code>exitCode</code> parameter will be the\npassed exit code. If the worker was terminated, the <code>exitCode</code> parameter will\nbe <code>1</code>.</p>",
              "type": "module",
              "displayName": "Event: `'exit'`"
            },
            {
              "textRaw": "Event: `'message'`",
              "name": "event:_`'message'`",
              "meta": {
                "added": [
                  "v10.5.0"
                ],
                "changes": []
              },
              "desc": "<ul>\n<li><code>value</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types\" class=\"type\">&lt;any&gt;</a> The transmitted value</li>\n</ul>\n<p>The <code>'message'</code> event is emitted when the worker thread has invoked\n<a href=\"#worker_threads_worker_postmessage_value_transferlist\"><code>require('worker_threads').parentPort.postMessage()</code></a>.\nSee the <a href=\"#worker_threads_event_message\"><code>port.on('message')</code></a> event for more details.</p>",
              "type": "module",
              "displayName": "Event: `'message'`"
            },
            {
              "textRaw": "Event: `'online'`",
              "name": "event:_`'online'`",
              "meta": {
                "added": [
                  "v10.5.0"
                ],
                "changes": []
              },
              "desc": "<p>The <code>'online'</code> event is emitted when the worker thread has started executing\nJavaScript code.</p>",
              "type": "module",
              "displayName": "Event: `'online'`"
            },
            {
              "textRaw": "`worker.postMessage(value[, transferList])`",
              "name": "`worker.postmessage(value[,_transferlist])`",
              "meta": {
                "added": [
                  "v10.5.0"
                ],
                "changes": []
              },
              "desc": "<ul>\n<li><code>value</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types\" class=\"type\">&lt;any&gt;</a></li>\n<li><code>transferList</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object\" class=\"type\">&lt;Object[]&gt;</a></li>\n</ul>\n<p>Send a message to the worker that will be received via\n<a href=\"#worker_threads_event_message\"><code>require('worker_threads').parentPort.on('message')</code></a>.\nSee <a href=\"#worker_threads_port_postmessage_value_transferlist\"><code>port.postMessage()</code></a> for more details.</p>",
              "type": "module",
              "displayName": "`worker.postMessage(value[, transferList])`"
            },
            {
              "textRaw": "`worker.ref()`",
              "name": "`worker.ref()`",
              "meta": {
                "added": [
                  "v10.5.0"
                ],
                "changes": []
              },
              "desc": "<p>Opposite of <code>unref()</code>, calling <code>ref()</code> on a previously <code>unref()</code>ed worker will\n<em>not</em> let the program exit if it's the only active handle left (the default\nbehavior). If the worker is <code>ref()</code>ed, calling <code>ref()</code> again will have\nno effect.</p>",
              "type": "module",
              "displayName": "`worker.ref()`"
            },
            {
              "textRaw": "`worker.resourceLimits`",
              "name": "`worker.resourcelimits`",
              "meta": {
                "added": [
                  "v12.16.0"
                ],
                "changes": []
              },
              "desc": "<ul>\n<li>\n<p><a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object\" class=\"type\">&lt;Object&gt;</a></p>\n<ul>\n<li><code>maxYoungGenerationSizeMb</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type\" class=\"type\">&lt;number&gt;</a></li>\n<li><code>maxOldGenerationSizeMb</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type\" class=\"type\">&lt;number&gt;</a></li>\n<li><code>codeRangeSizeMb</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type\" class=\"type\">&lt;number&gt;</a></li>\n</ul>\n</li>\n</ul>\n<p>Provides the set of JS engine resource constraints for this Worker thread.\nIf the <code>resourceLimits</code> option was passed to the <a href=\"#worker_threads_class_worker\"><code>Worker</code></a> constructor,\nthis matches its values.</p>\n<p>If the worker has stopped, the return value is an empty object.</p>",
              "type": "module",
              "displayName": "`worker.resourceLimits`"
            },
            {
              "textRaw": "`worker.stderr`",
              "name": "`worker.stderr`",
              "meta": {
                "added": [
                  "v10.5.0"
                ],
                "changes": []
              },
              "desc": "<ul>\n<li><a href=\"stream.html#stream_class_stream_readable\" class=\"type\">&lt;stream.Readable&gt;</a></li>\n</ul>\n<p>This is a readable stream which contains data written to <a href=\"process.html#process_process_stderr\"><code>process.stderr</code></a>\ninside the worker thread. If <code>stderr: true</code> was not passed to the\n<a href=\"#worker_threads_class_worker\"><code>Worker</code></a> constructor, then data will be piped to the parent thread's\n<a href=\"process.html#process_process_stderr\"><code>process.stderr</code></a> stream.</p>",
              "type": "module",
              "displayName": "`worker.stderr`"
            },
            {
              "textRaw": "`worker.stdin`",
              "name": "`worker.stdin`",
              "meta": {
                "added": [
                  "v10.5.0"
                ],
                "changes": []
              },
              "desc": "<ul>\n<li><a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type\" class=\"type\">&lt;null&gt;</a> | <a href=\"stream.html#stream_class_stream_writable\" class=\"type\">&lt;stream.Writable&gt;</a></li>\n</ul>\n<p>If <code>stdin: true</code> was passed to the <a href=\"#worker_threads_class_worker\"><code>Worker</code></a> constructor, this is a\nwritable stream. The data written to this stream will be made available in\nthe worker thread as <a href=\"process.html#process_process_stdin\"><code>process.stdin</code></a>.</p>",
              "type": "module",
              "displayName": "`worker.stdin`"
            },
            {
              "textRaw": "`worker.stdout`",
              "name": "`worker.stdout`",
              "meta": {
                "added": [
                  "v10.5.0"
                ],
                "changes": []
              },
              "desc": "<ul>\n<li><a href=\"stream.html#stream_class_stream_readable\" class=\"type\">&lt;stream.Readable&gt;</a></li>\n</ul>\n<p>This is a readable stream which contains data written to <a href=\"process.html#process_process_stdout\"><code>process.stdout</code></a>\ninside the worker thread. If <code>stdout: true</code> was not passed to the\n<a href=\"#worker_threads_class_worker\"><code>Worker</code></a> constructor, then data will be piped to the parent thread's\n<a href=\"process.html#process_process_stdout\"><code>process.stdout</code></a> stream.</p>",
              "type": "module",
              "displayName": "`worker.stdout`"
            },
            {
              "textRaw": "`worker.terminate()`",
              "name": "`worker.terminate()`",
              "meta": {
                "added": [
                  "v10.5.0"
                ],
                "changes": [
                  {
                    "version": "v12.5.0",
                    "pr-url": "https://github.com/nodejs/node/pull/28021",
                    "description": "This function now returns a Promise. Passing a callback is deprecated, and was useless up to this version, as the Worker was actually terminated synchronously. Terminating is now a fully asynchronous operation."
                  }
                ]
              },
              "desc": "<ul>\n<li>Returns: <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise\" class=\"type\">&lt;Promise&gt;</a></li>\n</ul>\n<p>Stop all JavaScript execution in the worker thread as soon as possible.\nReturns a Promise for the exit code that is fulfilled when the\n<a href=\"#worker_threads_event_exit\"><code>'exit'</code> event</a> is emitted.</p>",
              "type": "module",
              "displayName": "`worker.terminate()`"
            },
            {
              "textRaw": "`worker.threadId`",
              "name": "`worker.threadid`",
              "meta": {
                "added": [
                  "v10.5.0"
                ],
                "changes": []
              },
              "desc": "<ul>\n<li><a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type\" class=\"type\">&lt;integer&gt;</a></li>\n</ul>\n<p>An integer identifier for the referenced thread. Inside the worker thread,\nit is available as <a href=\"#worker_threads_worker_threadid\"><code>require('worker_threads').threadId</code></a>.\nThis value is unique for each <code>Worker</code> instance inside a single process.</p>",
              "type": "module",
              "displayName": "`worker.threadId`"
            },
            {
              "textRaw": "`worker.unref()`",
              "name": "`worker.unref()`",
              "meta": {
                "added": [
                  "v10.5.0"
                ],
                "changes": []
              },
              "desc": "<p>Calling <code>unref()</code> on a worker will allow the thread to exit if this is the only\nactive handle in the event system. If the worker is already <code>unref()</code>ed calling\n<code>unref()</code> again will have no effect.</p>",
              "type": "module",
              "displayName": "`worker.unref()`"
            }
          ],
          "type": "module",
          "displayName": "Class: `Worker`"
        }
      ],
      "type": "module",
      "displayName": "Worker Threads"
    }
  ]
}