Workers

worker_threads.isMainThread

Distinguishes the initial thread from a worker. It is not a check for process identity or privilege. Tested on Node.js 22.16.0. Example output (one line per entry): ["true false"].

Formal Syntax & Structure

nodejs syntaxnodejs
isMainThread

Return Value

boolean

True in main thread.

Working Example

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