Node.js

- About these Docs
- Usage & Example

- Assertion Testing
- Async Hooks
- Buffer
- C++ Addons
- C/C++ Addons - N-API
- Child Processes
- Cluster
- Command Line Options
- Console
- Crypto
- Debugger
- Deprecated APIs
- DNS
- Domain
- ECMAScript Modules
- Errors
- Events
- File System
- Globals
- HTTP
- HTTP/2
- HTTPS
- Inspector
- Internationalization
- Modules
- Net
- OS
- Path
- Performance Hooks
- Policies
- Process
- Punycode
- Query Strings
- Readline
- REPL
- Report
- Stream
- String Decoder
- Timers
- TLS/SSL
- Trace Events
- TTY
- UDP/Datagram
- URL
- Utilities
- V8
- VM
- Worker Threads
- Zlib

- GitHub Repo & Issue Tracker

# Worker Threads#

Stability: 1 - Experimental

The `worker_threads` module enables the use of threads that execute JavaScript in parallel. To access it:

    const worker = require('worker_threads');

Workers (threads) are useful for performing CPU-intensive JavaScript operations. They will not help much with I/O-intensive work. Node.js’s built-in asynchronous I/O operations are more efficient than Workers can be.

Unlike `child_process` or `cluster`, `worker_threads` can share memory. They do so by transferring `ArrayBuffer` instances or sharing `SharedArrayBuffer` instances.

    const {
      Worker, isMainThread, parentPort, workerData
    } = require('worker_threads');

    if (isMainThread) {
      module.exports = function parseJSAsync(script) {
        return new Promise((resolve, reject) => {
          const worker = new Worker(__filename, {
            workerData: script
          });
          worker.on('message', resolve);
          worker.on('error', reject);
          worker.on('exit', (code) => {
            if (code !== 0)
              reject(new Error(`Worker stopped with exit code ${code}`));
          });
        });
      };
    } else {
      const { parse } = require('some-js-parsing-library');
      const script = workerData;
      parentPort.postMessage(parse(script));
    }

The above example spawns a Worker thread for each `parse()` call. In actual practice, use a pool of Workers instead for these kinds of tasks. Otherwise, the overhead of creating Workers would likely exceed their benefit.

## worker.isMainThread#

Added in: v10.5.0

- <boolean>

Is `true` if this code is not running inside of a `Worker` thread.

    const { Worker, isMainThread } = require('worker_threads');

    if (isMainThread) {
      // This re-loads the current file inside a Worker instance.
      new Worker(__filename);
    } else {
      console.log('Inside Worker!');
      console.log(isMainThread);  // Prints 'false'.
    }

## worker.moveMessagePortToContext(port, contextifiedSandbox)#

Added in: v11.13.0

- `port` <MessagePort> The message port which will be transferred.

- `contextifiedSandbox` <Object> A contextified object as returned by the `vm.createContext()` method.

- Returns: <MessagePort>

Transfer a `MessagePort` to a different `vm` Context. The original `port` object will be rendered unusable, and the returned `MessagePort` instance will take its place.

The returned `MessagePort` will be an object in the target context, and will inherit from its global `Object` class. Objects passed to the `port.onmessage()` listener will also be created in the target context and inherit from its global `Object` class.

However, the created `MessagePort` will no longer inherit from `EventEmitter`, and only `port.onmessage()` can be used to receive events using it.

## worker.parentPort#

Added in: v10.5.0

- <null> | <MessagePort>

If this thread was spawned as a `Worker`, this will be a `MessagePort` allowing communication with the parent thread. Messages sent using `parentPort.postMessage()` will be available in the parent thread using `worker.on('message')`, and messages sent from the parent thread using `worker.postMessage()` will be available in this thread using `parentPort.on('message')`.

    const { Worker, isMainThread, parentPort } = require('worker_threads');

    if (isMainThread) {
      const worker = new Worker(__filename);
      worker.once('message', (message) => {
        console.log(message);  // Prints 'Hello, world!'.
      });
      worker.postMessage('Hello, world!');
    } else {
      // When a message from the parent thread is received, send it back:
      parentPort.once('message', (message) => {
        parentPort.postMessage(message);
      });
    }

## worker.threadId#

Added in: v10.5.0

- <integer>

An integer identifier for the current thread. On the corresponding worker object (if there is any), it is available as `worker.threadId`. This value is unique for each `Worker` instance inside a single process.

## worker.workerData#

Added in: v10.5.0

An arbitrary JavaScript value that contains a clone of the data passed to this thread’s `Worker` constructor.

The data is cloned as if using `postMessage()`, according to the HTML structured clone algorithm.

    const { Worker, isMainThread, workerData } = require('worker_threads');

    if (isMainThread) {
      const worker = new Worker(__filename, { workerData: 'Hello, world!' });
    } else {
      console.log(workerData);  // Prints 'Hello, world!'.
    }

## Class: MessageChannel#

Added in: v10.5.0

Instances of the `worker.MessageChannel` class represent an asynchronous, two-way communications channel. The `MessageChannel` has no methods of its own. `new MessageChannel()` yields an object with `port1` and `port2` properties, which refer to linked `MessagePort` instances.

    const { MessageChannel } = require('worker_threads');

    const { port1, port2 } = new MessageChannel();
    port1.on('message', (message) => console.log('received', message));
    port2.postMessage({ foo: 'bar' });
    // Prints: received { foo: 'bar' } from the `port1.on('message')` listener

## Class: MessagePort#

Added in: v10.5.0

- Extends: <EventEmitter>

Instances of the `worker.MessagePort` class represent one end of an asynchronous, two-way communications channel. It can be used to transfer structured data, memory regions and other `MessagePort`s between different `Worker`s.

With the exception of `MessagePort`s being `EventEmitter`s rather than `EventTarget`s, this implementation matches browser `MessagePort`s.

### Event: 'close'#

Added in: v10.5.0

The `'close'` event is emitted once either side of the channel has been disconnected.

    const { MessageChannel } = require('worker_threads');
    const { port1, port2 } = new MessageChannel();

    // Prints:
    //   foobar
    //   closed!
    port2.on('message', (message) => console.log(message));
    port2.on('close', () => console.log('closed!'));

    port1.postMessage('foobar');
    port1.close();

### Event: 'message'#

Added in: v10.5.0

- `value` <any> The transmitted value

The `'message'` event is emitted for any incoming message, containing the cloned input of `port.postMessage()`.

Listeners on this event will receive a clone of the `value` parameter as passed to `postMessage()` and no further arguments.

### port.close()#

Added in: v10.5.0

Disables further sending of messages on either side of the connection. This method can be called when no further communication will happen over this `MessagePort`.

The `'close'` event will be emitted on both `MessagePort` instances that are part of the channel.

### port.postMessage(value[, transferList])#

Added in: v10.5.0

- `value` <any>
- `transferList` <Object[]>

Sends a JavaScript value to the receiving side of this channel. `value` will be transferred in a way which is compatible with the HTML structured clone algorithm.

In particular, the significant differences to `JSON` are:

- `value` may contain circular references.
- `value` may contain instances of builtin JS types such as `RegExp`s, `BigInt`s, `Map`s, `Set`s, etc.
- `value` may contained typed arrays, both using `ArrayBuffer`s and `SharedArrayBuffer`s.
- `value` may contain `WebAssembly.Module` instances.
- `value` may not contain native (C++-backed) objects other than `MessagePort`s.

    const { MessageChannel } = require('worker_threads');
    const { port1, port2 } = new MessageChannel();

    port1.on('message', (message) => console.log(message));

    const circularData = {};
    circularData.foo = circularData;
    // Prints: { foo: [Circular] }
    port2.postMessage(circularData);

`transferList` may be a list of `ArrayBuffer` and `MessagePort` objects. After transferring, they will not be usable on the sending side of the channel anymore (even if they are not contained in `value`). Unlike with child processes, transferring handles such as network sockets is currently not supported.

If `value` contains `SharedArrayBuffer` instances, those will be accessible from either thread. They cannot be listed in `transferList`.

`value` may still contain `ArrayBuffer` instances that are not in `transferList`; in that case, the underlying memory is copied rather than moved.

    const { MessageChannel } = require('worker_threads');
    const { port1, port2 } = new MessageChannel();

    port1.on('message', (message) => console.log(message));

    const uint8Array = new Uint8Array([ 1, 2, 3, 4 ]);
    // This posts a copy of `uint8Array`:
    port2.postMessage(uint8Array);
    // This does not copy data, but renders `uint8Array` unusable:
    port2.postMessage(uint8Array, [ uint8Array.buffer ]);

    // The memory for the `sharedUint8Array` will be accessible from both the
    // original and the copy received by `.on('message')`:
    const sharedUint8Array = new Uint8Array(new SharedArrayBuffer(4));
    port2.postMessage(sharedUint8Array);

    // This transfers a freshly created message port to the receiver.
    // This can be used, for example, to create communication channels between
    // multiple `Worker` threads that are children of the same parent thread.
    const otherChannel = new MessageChannel();
    port2.postMessage({ port: otherChannel.port1 }, [ otherChannel.port1 ]);

Because the object cloning uses the structured clone algorithm, non-enumerable properties, property accessors, and object prototypes are not preserved. In particular, `Buffer` objects will be read as plain `Uint8Array`s on the receiving side.

The message object will be cloned immediately, and can be modified after posting without having side effects.

For more information on the serialization and deserialization mechanisms behind this API, see the serialization API of the `v8` module.

### port.ref()#

Added in: v10.5.0

Opposite of `unref()`. Calling `ref()` on a previously `unref()`ed port will not let the program exit if it's the only active handle left (the default behavior). If the port is `ref()`ed, calling `ref()` again will have no effect.

If listeners are attached or removed using `.on('message')`, the port will be `ref()`ed and `unref()`ed automatically depending on whether listeners for the event exist.

### port.start()#

Added in: v10.5.0

Starts receiving messages on this `MessagePort`. When using this port as an event emitter, this will be called automatically once `'message'` listeners are attached.

This method exists for parity with the Web `MessagePort` API. In Node.js, it is only useful for ignoring messages when no event listener is present. Node.js also diverges in its handling of `.onmessage`. Setting it will automatically call `.start()`, but unsetting it will let messages queue up until a new handler is set or the port is discarded.

### port.unref()#

Added in: v10.5.0

Calling `unref()` on a port will allow the thread to exit if this is the only active handle in the event system. If the port is already `unref()`ed calling `unref()` again will have no effect.

If listeners are attached or removed using `.on('message')`, the port will be `ref()`ed and `unref()`ed automatically depending on whether listeners for the event exist.

## Class: Worker#

Added in: v10.5.0

- Extends: <EventEmitter>

The `Worker` class represents an independent JavaScript execution thread. Most Node.js APIs are available inside of it.

Notable differences inside a Worker environment are:

- The `process.stdin`, `process.stdout` and `process.stderr` may be redirected by the parent thread.
- The `require('worker_threads').isMainThread` property is set to `false`.
- The `require('worker_threads').parentPort` message port is available.
- `process.exit()` does not stop the whole program, just the single thread, and `process.abort()` is not available.
- `process.chdir()` and `process` methods that set group or user ids are not available.
- `process.env` is a read-only reference to the environment variables.
- `process.title` cannot be modified.
- Signals will not be delivered through `process.on('...')`.
- Execution may stop at any point as a result of `worker.terminate()` being invoked.
- IPC channels from parent processes are not accessible.
- The `trace_events` module is not supported.
- Native add-ons can only be loaded from multiple threads if they fulfill certain conditions.

Creating `Worker` instances inside of other `Worker`s is possible.

Like Web Workers and the `cluster` module, two-way communication can be achieved through inter-thread message passing. Internally, a `Worker` has a built-in pair of `MessagePort`s that are already associated with each other when the `Worker` is created. While the `MessagePort` object on the parent side is not directly exposed, its functionalities are exposed through `worker.postMessage()` and the `worker.on('message')` event on the `Worker` object for the parent thread.

To create custom messaging channels (which is encouraged over using the default global channel because it facilitates separation of concerns), users can create a `MessageChannel` object on either thread and pass one of the `MessagePort`s on that `MessageChannel` to the other thread through a pre-existing channel, such as the global one.

See `port.postMessage()` for more information on how messages are passed, and what kind of JavaScript values can be successfully transported through the thread barrier.

    const assert = require('assert');
    const {
      Worker, MessageChannel, MessagePort, isMainThread, parentPort
    } = require('worker_threads');
    if (isMainThread) {
      const worker = new Worker(__filename);
      const subChannel = new MessageChannel();
      worker.postMessage({ hereIsYourPort: subChannel.port1 }, [subChannel.port1]);
      subChannel.port2.on('message', (value) => {
        console.log('received:', value);
      });
    } else {
      parentPort.once('message', (value) => {
        assert(value.hereIsYourPort instanceof MessagePort);
        value.hereIsYourPort.postMessage('the worker is sending this');
        value.hereIsYourPort.close();
      });
    }

### new Worker(filename[, options])#

- `filename` <string> The path to the Worker’s main script. Must be either an absolute path or a relative path (i.e. relative to the current working directory) starting with `./` or `../`. If `options.eval` is `true`, this is a string containing JavaScript code rather than a path.

- `options` <Object>

  - `eval` <boolean> If `true`, interpret the first argument to the constructor as a script that is executed once the worker is online.
  - `workerData` <any> Any JavaScript value that will be cloned and made available as `require('worker_threads').workerData`. The cloning will occur as described in the HTML structured clone algorithm, and an error will be thrown if the object cannot be cloned (e.g. because it contains `function`s).
  - `stdin` <boolean> If this is set to `true`, then `worker.stdin` will provide a writable stream whose contents will appear as `process.stdin` inside the Worker. By default, no data is provided.
  - `stdout` <boolean> If this is set to `true`, then `worker.stdout` will not automatically be piped through to `process.stdout` in the parent.
  - `stderr` <boolean> If this is set to `true`, then `worker.stderr` will not automatically be piped through to `process.stderr` in the parent.
  - `execArgv` <string[]> List of node CLI options passed to the worker. V8 options (such as `--max-old-space-size`) and options that affect the process (such as `--title`) are not supported. If set, this will be provided as `process.execArgv` inside the worker. By default, options will be inherited from the parent thread.

### Event: 'error'#

Added in: v10.5.0

- `err` <Error>

The `'error'` event is emitted if the worker thread throws an uncaught exception. In that case, the worker will be terminated.

### Event: 'exit'#

Added in: v10.5.0

- `exitCode` <integer>

The `'exit'` event is emitted once the worker has stopped. If the worker exited by calling `process.exit()`, the `exitCode` parameter will be the passed exit code. If the worker was terminated, the `exitCode` parameter will be `1`.

### Event: 'message'#

Added in: v10.5.0

- `value` <any> The transmitted value

The `'message'` event is emitted when the worker thread has invoked `require('worker_threads').parentPort.postMessage()`. See the `port.on('message')` event for more details.

### Event: 'online'#

Added in: v10.5.0

The `'online'` event is emitted when the worker thread has started executing JavaScript code.

### worker.postMessage(value[, transferList])#

Added in: v10.5.0

- `value` <any>
- `transferList` <Object[]>

Send a message to the worker that will be received via `require('worker_threads').parentPort.on('message')`. See `port.postMessage()` for more details.

### worker.ref()#

Added in: v10.5.0

Opposite of `unref()`, calling `ref()` on a previously `unref()`ed worker will not let the program exit if it's the only active handle left (the default behavior). If the worker is `ref()`ed, calling `ref()` again will have no effect.

### worker.stderr#

Added in: v10.5.0

- <stream.Readable>

This is a readable stream which contains data written to `process.stderr` inside the worker thread. If `stderr: true` was not passed to the `Worker` constructor, then data will be piped to the parent thread's `process.stderr` stream.

### worker.stdin#

Added in: v10.5.0

- <null> | <stream.Writable>

If `stdin: true` was passed to the `Worker` constructor, this is a writable stream. The data written to this stream will be made available in the worker thread as `process.stdin`.

### worker.stdout#

Added in: v10.5.0

- <stream.Readable>

This is a readable stream which contains data written to `process.stdout` inside the worker thread. If `stdout: true` was not passed to the `Worker` constructor, then data will be piped to the parent thread's `process.stdout` stream.

### worker.terminate([callback])#

Added in: v10.5.0

- `callback` <Function>

  - `err` <Error>
  - `exitCode` <integer>

Stop all JavaScript execution in the worker thread as soon as possible. `callback` is an optional function that is invoked once this operation is known to have completed.

Warning: Currently, not all code in the internals of Node.js is prepared to expect termination at arbitrary points in time and may crash if it encounters that condition. Consequently, only call `.terminate()` if it is known that the Worker thread is not accessing Node.js core modules other than what is exposed in the `worker` module.

### worker.threadId#

Added in: v10.5.0

- <integer>

An integer identifier for the referenced thread. Inside the worker thread, it is available as `require('worker_threads').threadId`. This value is unique for each `Worker` instance inside a single process.

### worker.unref()#

Added in: v10.5.0

Calling `unref()` on a worker will allow the thread to exit if this is the only active handle in the event system. If the worker is already `unref()`ed calling `unref()` again will have no effect.