class Worker 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, andprocess.stderrstreams may be redirected by the parent thread. - The
require('node:worker_threads').isMainThreadproperty is set tofalse. - The
require('node:worker_threads').parentPortmessage port is available. process.exit()does not stop the whole program, just the single thread, andprocess.abort()is not available.process.chdir()andprocessmethods that set group or user ids are not available.process.envis a copy of the parent thread's environment variables, unless otherwise specified. Changes to one copy are not visible in other threads, and are not visible to native add-ons (unlessworker.SHARE_ENVis passed as theenvoption to theWorkerconstructor). On Windows, unlike the main thread, a copy of the environment variables operates in a case-sensitive manner.process.titlecannot be modified.- Signals are not 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_eventsmodule is not supported. - Native add-ons can only be loaded from multiple threads if they fulfill certain conditions.
Creating Worker instances inside of other Workers is possible.
Like Web Workers and the node:cluster module, two-way communication
can be achieved through inter-thread message passing. Internally, a Worker has
a built-in pair of MessagePorts 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
MessagePorts 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.
import assert from 'node:assert'; import { Worker, MessageChannel, MessagePort, isMainThread, parentPort, } from 'node:worker_threads'; if (isMainThread) { const worker = new Worker(new URL(import.meta.url)); 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(); }); }
const assert = require('node:assert'); const { Worker, MessageChannel, MessagePort, isMainThread, parentPort, } = require('node: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(); }); }
Worker Constructor
History
name option, which allows adding a name to worker title for debugging.filename parameter can be a WHATWG URL object using data: protocol.trackUnmanagedFds option was set to true by default.trackUnmanagedFds option was introduced.transferList option was introduced.filename parameter can be a WHATWG URL object using file: protocol.argv option was introduced.resourceLimits option was introduced.new Worker(filename, options?): Worker
./ or ../, or a WHATWG URL
object using file: or data: protocol.
When using a data: URL, the data is interpreted based on MIME type using
the ECMAScript module loader.
If options.eval is true, this is a string containing JavaScript code
rather than a path.Objectany[]process.argv in the worker. This is mostly similar to the workerData
but the values are available on the global process.argv as if they
were passed as CLI options to the script.Objectprocess.env inside
the Worker thread. As a special value, worker.SHARE_ENV may be used
to specify that the parent thread and the child thread should share their
environment variables; in that case, changes to one thread's process.env
object affect the other thread as well. Default: process.env.booleantrue and the first argument is a string, interpret
the first argument to the constructor as a script that is executed once the
worker is online.string[]--max-old-space-size) and options that affect the
process (such as --title) are not supported. If set, this is provided
as process.execArgv inside the worker. By default, options are
inherited from the parent thread.booleantrue, then worker.stdin
provides a writable stream whose contents appear as process.stdin
inside the Worker. By default, no data is provided.booleantrue, then worker.stdout is
not automatically piped through to process.stdout in the parent.booleantrue, then worker.stderr is
not automatically piped through to process.stderr in the parent.anyrequire('node:worker_threads').workerData. The cloning
occurs as described in the HTML structured clone algorithm, and an error
is thrown if the object cannot be cloned (e.g. because it contains
functions).booleantrue, then the Worker
tracks raw file descriptors managed through fs.open() and
fs.close(), and closes them when the Worker exits, similar to other
resources like network sockets or file descriptors managed through
the FileHandle API. This option is automatically inherited by all
nested Workers. Default: true.Object[]MessagePort-like objects
are passed in workerData, a transferList is required for those
items or ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST is thrown.
See port.postMessage() for more information.ObjectWorker
instance. These limits only affect the JS engine, and no external data,
including no ArrayBuffers. Even if these limits are set, the process may
still abort if it encounters a global out-of-memory situation.number--max-old-space-size is set, it
overrides this setting.number--max-semi-space-size is set, it overrides this setting.numbernumber4.stringname to be replaced in the thread name
and to the worker title for debugging/identification purposes,
making the final title as [worker ${id}] ${name}.
This parameter has a maximum allowed size, depending on the operating
system. If the provided name exceeds the limit, it will be truncatedanyThe 'error' event is emitted if the worker thread throws an uncaught
exception. In that case, the worker is terminated.
integerThe 'exit' event is emitted once the worker has stopped. If the worker
exited by calling process.exit(), the exitCode parameter is the
passed exit code. If the worker was terminated, the exitCode parameter is
1.
This is the final event emitted by any Worker instance.
anyThe 'message' event is emitted when the worker thread has invoked
require('node:worker_threads').parentPort.postMessage().
See the port.on('message') event for more details.
All messages sent from the worker thread are emitted before the
'exit' event is emitted on the Worker object.
ErrorThe 'messageerror' event is emitted when deserializing a message failed.
The 'online' event is emitted when the worker thread has started executing
JavaScript code.
worker.cpuUsage(prev?): Promise
PromiseThis method returns a Promise that will resolve to an object identical to process.threadCpuUsage(),
or reject with an ERR_WORKER_NOT_RUNNING error if the worker is no longer running.
This methods allows the statistics to be observed from outside the actual thread.
worker.getHeapSnapshot
History
worker.getHeapSnapshot(options?): Promise
Returns a readable stream for a V8 snapshot of the current state of the Worker.
See v8.getHeapSnapshot() for more details.
If the Worker thread is no longer running, which may occur before the
'exit' event is emitted, the returned Promise is rejected
immediately with an ERR_WORKER_NOT_RUNNING error.
worker.getHeapStatistics(): Promise
PromiseThis method returns a Promise that will resolve to an object identical to v8.getHeapStatistics(),
or reject with an ERR_WORKER_NOT_RUNNING error if the worker is no longer running.
This methods allows the statistics to be observed from outside the actual thread.
An object that can be used to query performance information from a worker instance.
performance.eventLoopUtilization
History
performance.eventLoopUtilization(utilization1?, utilization2?): Object
The same call as perf_hooks eventLoopUtilization(), except the values
of the worker instance are returned.
One difference is that, unlike the main thread, bootstrapping within a worker is done within the event loop. So the event loop utilization is immediately available once the worker's script begins execution.
An idle time that does not increase does not indicate that the worker is
stuck in bootstrap. The following example shows how the worker's entire
lifetime never accumulates any idle time, but is still able to process
messages.
import { Worker, isMainThread, parentPort } from 'node:worker_threads'; if (isMainThread) { const worker = new Worker(new URL(import.meta.url)); setInterval(() => { worker.postMessage('hi'); console.log(worker.performance.eventLoopUtilization()); }, 100).unref(); } else { parentPort.on('message', () => console.log('msg')).unref(); (function r(n) { if (--n < 0) return; const t = Date.now(); while (Date.now() - t < 300); setImmediate(r, n); })(10); }
const { Worker, isMainThread, parentPort } = require('node:worker_threads'); if (isMainThread) { const worker = new Worker(__filename); setInterval(() => { worker.postMessage('hi'); console.log(worker.performance.eventLoopUtilization()); }, 100).unref(); } else { parentPort.on('message', () => console.log('msg')).unref(); (function r(n) { if (--n < 0) return; const t = Date.now(); while (Date.now() - t < 300); setImmediate(r, n); })(10); }
The event loop utilization of a worker is available only after the 'online'
event emitted, and if called before this, or after the 'exit'
event, then all properties have the value of 0.
worker.postMessage(value, transferList?): void
Send a message to the worker that is received via
require('node:worker_threads').parentPort.on('message').
See port.postMessage() for more details.
worker.ref(): void
Opposite of unref(), calling ref() on a previously unref()ed worker does
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 has
no effect.
Provides the set of JS engine resource constraints for this Worker thread.
If the resourceLimits option was passed to the Worker constructor,
this matches its values.
If the worker has stopped, the return value is an empty object.
worker.startCpuProfile(options?): Promise
Starting a CPU profile then return a Promise that fulfills with an error
or an CPUProfileHandle object. This API supports await using syntax.
const { Worker } = require('node:worker_threads'); const worker = new Worker(` const { parentPort } = require('worker_threads'); parentPort.on('message', () => {}); `, { eval: true }); worker.on('online', async () => { const handle = await worker.startCpuProfile({ sampleInterval: 1 }); const profile = await handle.stop(); console.log(profile); worker.terminate(); });
await using example.
const { Worker } = require('node:worker_threads'); const w = new Worker(` const { parentPort } = require('node:worker_threads'); parentPort.on('message', () => {}); `, { eval: true }); w.on('online', async () => { // Stop profile automatically when return and profile will be discarded await using handle = await w.startCpuProfile(); });
worker.startHeapProfile(options?): Promise
Objectnumber524288 (512 KiB).integer16.booleanfalse.booleanfalse.booleanfalse.PromiseStarting a Heap profile then return a Promise that fulfills with an error
or an HeapProfileHandle object. This API supports await using syntax.
const { Worker } = require('node:worker_threads'); const worker = new Worker(` const { parentPort } = require('worker_threads'); parentPort.on('message', () => {}); `, { eval: true }); worker.on('online', async () => { const handle = await worker.startHeapProfile(); const profile = await handle.stop(); console.log(profile); worker.terminate(); });
import { Worker } from 'node:worker_threads'; const worker = new Worker(` const { parentPort } = require('node:worker_threads'); parentPort.on('message', () => {}); `, { eval: true }); worker.on('online', async () => { const handle = await worker.startHeapProfile(); const profile = await handle.stop(); console.log(profile); worker.terminate(); });
await using example.
const { Worker } = require('node:worker_threads'); const w = new Worker(` const { parentPort } = require('node:worker_threads'); parentPort.on('message', () => {}); `, { eval: true }); w.on('online', async () => { // Stop profile automatically when return and profile will be discarded await using handle = await w.startHeapProfile(); });
import { Worker } from 'node:worker_threads'; const w = new Worker(` const { parentPort } = require('node:worker_threads'); parentPort.on('message', () => {}); `, { eval: true }); w.on('online', async () => { // Stop profile automatically when return and profile will be discarded await using handle = await w.startHeapProfile(); });
stream.ReadableThis 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 is piped to the parent thread's
process.stderr stream.
null | stream.WritableIf 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.
stream.ReadableThis 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 is piped to the parent thread's
process.stdout stream.
worker.terminate(): Promise
PromiseStop all JavaScript execution in the worker thread as soon as possible.
Returns a Promise for the exit code that is fulfilled when the
'exit' event is emitted.
integerAn integer identifier for the referenced thread. Inside the worker thread,
it is available as require('node:worker_threads').threadId.
This value is unique for each Worker instance inside a single process.
A string identifier for the referenced thread or null if the thread is not running.
Inside the worker thread, it is available as require('node:worker_threads').threadName.
worker.unref(): void
Calling unref() on a worker allows 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 has no effect.
worker[Symbol.asyncDispose](): void
Calls worker.terminate() when the dispose scope is exited.
async function example() { await using worker = new Worker('for (;;) {}', { eval: true }); // Worker is automatically terminate when the scope is exited. }