Workers

worker_threads.threadId

Identifies a thread within its process. Do not treat it as a durable request ID or globally unique service identifier. Tested on Node.js 22.16.0. Example output (one line per entry): ["true true"].

Formal Syntax & Structure

nodejs syntaxnodejs
threadId

Return Value

number

Thread identifier.

Working Example

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