`styleText()` doesn't work within a worker thread
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 122k
- Forks
- 37.3k
- Avg merge
- 4d 2h
- Merged PRs (30d)
- 283
Description
Version
26.8.1
Platform
Darwin MacBook-Pro-de-Sebastien.local 24.6.0 Darwin Kernel Version 24.6.0: Tue Apr 21 20:16:56 PDT 2026; root:xnu-11417.140.69.710.16~1/RELEASE_ARM64_T6030 arm64
Subsystem
No response
What steps will reproduce the bug?
const {Worker} = require('node:worker_threads');
const {styleText} = require('node:util');
const tty = require('node:tty');
console.log('MAIN', {
isTTY: process.stdout.isTTY,
isatty: tty.isatty(1),
styled: JSON.stringify(styleText('red', 'hello')),
});
await new Promise((resolve, reject) => {
const worker = new Worker(`
const {parentPort} = require('node:worker_threads');
const {styleText} = require('node:util');
const tty = require('node:tty');
parentPort.postMessage({
isTTY: process.stdout.isTTY,
isatty: tty.isatty(1),
styled: JSON.stringify(styleText('red', 'hello')),
});
`, {eval: true});
worker.on('message', msg => {
console.log('WORKER', msg);
resolve();
});
worker.on('error', reject);
});
Output:
MAIN {
isTTY: true,
isatty: true,
styled: '"\\u001b[31mhello\\u001b[39m"'
}
WORKER {
isTTY: undefined,
isatty: true,
styled: '"hello"'
}
Even simpler repro:
const { Worker } = require('node:worker_threads');
const { styleText } = require('node:util');
console.log('main:', styleText('red', 'hello'));
new Worker(`
const { styleText } = require('node:util');
console.log('worker:', styleText('red', 'hello'));
`, { eval: true });
How often does it reproduce? Is there a required condition?
constantly
What is the expected behavior? Why is that the expected behavior?
I think it's reasonable to expect styleText() to also work in worker threads by default if the parent has colors enabled. I'd rather have an opt-out than an opt-in through worker thread config/env.
What do you see instead?
The worker thread
Additional information
From what I understand, the heuristic used in styleText() is too limited and could be improved to support colors in worker threads better
- https://github.com/nodejs/node/blob/main/lib/util.js
- https://github.com/nodejs/node/blob/main/lib/internal/util/colors.js
In chalk, this works out of the box, based on https://github.com/chalk/supports-color
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the worker_threads example first, then inspect lib/util.js and lib/internal/util/colors.js, which the report identifies as the relevant heuristic. Compare color detection in the main thread and worker, and consider the parent-process color setting described in the expected behavior. Done means styleText() produces the expected colored output in a worker when colors are enabled by the parent.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, nodejs
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 68/100