Workers
worker_threads.setEnvironmentData()
Sets data cloned into newly started workers. It is distinct from process environment variables and does not update already running workers. Tested on Node.js 22.16.0. Example output (one line per entry): ["4"].
Formal Syntax & Structure
nodejs syntaxnodejs
setEnvironmentData(key[, value])Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| key | unknown | Cloneable lookup key. |
| value | unknown | Cloneable value. |
Return Value
undefined
Sets or deletes data.
Working Example
Example Snippetnodejs
import { Worker, setEnvironmentData } from 'node:worker_threads';import { once } from 'node:events';setEnvironmentData('lab', { limit: 4 });const w = new Worker("const t=require('node:worker_threads');t.parentPort.postMessage(t.getEnvironmentData('lab').limit)", { eval: true });const exit = once(w, 'exit');try { console.log((await once(w, 'message'))[0]); await exit;}finally { setEnvironmentData('lab'); await w.terminate();}