Color not enabled when in a Node Worker
See original GitHub issueThis is related to an ongoing issue/discussion in Node.js related to process.stdout
and process.stderr
not having isTTY === true
when in a Node.js Worker thread.
This means that, if debug
is used within a Worker thread, it does not print colors nor ms diff (but timestamps).
Code to reproduce it (requires Node >= 12 or Node 10 with node --experimental-worker
flag):
foo.js:
process.env.DEBUG = '*';
const { Worker, parentPort, threadId, isMainThread } = require('worker_threads');
const tty = require('tty');
const debug = require('debug');
async function run()
{
if (isMainThread)
{
const logger = debug('[parent]');
logger(
'run() in main thread [process.stderr.isTTY:%s, tty.isatty():%s]',
process.stderr.isTTY,
tty.isatty(process.stderr.fd));
const worker = new Worker(__filename);
worker.once('message', message => logger('message from child: %o', message));
worker.postMessage('hi child!');
}
else
{
const logger = debug('[child]');
logger(
'run() in child thread [process.stderr.isTTY:%s, tty.isatty():%s]',
process.stderr.isTTY,
tty.isatty(process.stderr.fd));
parentPort.once('message', message => logger('message from parent: %o', message));
parentPort.postMessage('hi dad!');
}
}
run();
Output:
$ node --experimental-worker foo.js
[parent] run() in main thread [process.stderr.isTTY:true, tty.isatty():true] +0ms
2020-01-12T15:22:36.083Z [child] run() in child thread [process.stderr.isTTY:undefined, tty.isatty():false]
[parent] message from child: 'hi dad!' +33ms
2020-01-12T15:22:36.085Z [child] message from parent: 'hi child!'
Of course it works if I set DEBUG_COLORS=true
environment variable. Just wondering if a debug instance may expose a method to set “tty” mode.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:8 (8 by maintainers)
Top Results From Across the Web
Color not enabled when in a Node Worker · Issue #739 - GitHub
js Worker thread. This means that, if debug is used within a Worker thread, it does not print colors nor ms diff (but...
Read more >debug - npm
In Node.js, colors are enabled when stderr is a TTY. You also should install the supports-color module alongside debug, otherwise debug will ...
Read more >Manage nodes in a swarm - Docker Documentation
The MANAGER STATUS column shows node participation in the Raft consensus: No value indicates a worker node that does not participate in swarm...
Read more >Command-line API | Node.js v19.3.0 Documentation
If the string is not an absolute path, it's resolved as a relative path from the current working directory. That path is then...
Read more >Nodes stats API | Elasticsearch Guide [8.5] | Elastic
A value of -1 indicates that this is not available. indexing. (object) Contains statistics about indexing operations for the node. Properties of indexing....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The problem is that this may not even be “fixed” in Node. From the referenced issue:
I’d be happy by just manually setting
process.stderr.isTTY = true
in the Worker thread. However this won’t make debug module produce colors since it looks attty.isatty()
.And another super ugly workaround is (in the top of the app):
Thanks for the explanation.