Workers

worker_threads.workerData

Receives the initial structured-cloned worker input. Later parent mutations do not update this ordinary object. Validate message shape at the receiving boundary. Tested on Node.js 22.16.0. Example output (one line per entry): ["3"].

Formal Syntax & Structure

nodejs syntaxnodejs
workerData

Return Value

unknown

Initial cloned input.

Working Example

Example Snippetnodejs
import { Worker } from 'node:worker_threads';import { once } from 'node:events';const input = { quantity: 3 };const w = new Worker("const t=require('node:worker_threads');t.parentPort.postMessage(t.workerData.quantity)", { eval: true, workerData: input });const exit = once(w, 'exit');input.quantity = 9;try {    console.log((await once(w, 'message'))[0]);    await exit;}finally {    await w.terminate();}
Node.js Reference