You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
process.stdout/stderr.isTTY undefined in worker_thread #26946
I am not sure if this is intended behavior or not, but I found it while looking into why supports-color reports that color is not supported from worker threads (see chalk/supports-color#97). If I ignore the isTTY flag, then color output does seem to work correctly.
IMO it make sense for .isTTY to be undefined because process.stdout and process.stderr in worker threads don't actually write to the TTY. They postMessage() the data to the main thread which then writes it out on the worker thread's behalf.
We could copy the .isTTY values from the main thread but that breaks the idiom where if (process.stdout.isTTY) guards are used around calls to process.stdout.setRawMode(true) or process.stdout.cursorTo() - and you can't reasonably support those APIs in worker threads because the TTY is a per-process resource.
@silverwind For child_process (cluster and fork) it is possible to pass current process stdio using the third argument ({ stdout: process.stdout }) and it preserves isTTY but for threads, we can't.
As a (temporary) workaround, we can set workerData: { isTTY: process.stdout.isTTY }, when creating Worker and in very beginning of worker script:
We could copy the .isTTY values from the main thread but that breaks the idiom where if (process.stdout.isTTY) guards are used around calls to process.stdout.setRawMode(true) or process.stdout.cursorTo()
@bnoordhuis It would be useful to be able to know whether the main thread is TTY though. When you know you don't need things that are unsupported. For example, it could be useful to decide whether to print ANSI colors from the worker thread. Could Node.js somehow expose to the worker thread whether stdout/stderr in the main thread is TTY?
Reacted by Nicolò Ribaudo and Darien Maillet Valentine
Activity
BridgeAR commentedon Mar 27, 2019
// cc @addaleax
bnoordhuis commentedon Mar 28, 2019
IMO it make sense for
.isTTYto be undefined becauseprocess.stdoutandprocess.stderrin worker threads don't actually write to the TTY. They postMessage() the data to the main thread which then writes it out on the worker thread's behalf.We could copy the
.isTTYvalues from the main thread but that breaks the idiom whereif (process.stdout.isTTY)guards are used around calls toprocess.stdout.setRawMode(true)orprocess.stdout.cursorTo()- and you can't reasonably support those APIs in worker threads because the TTY is a per-process resource.silverwind commentedon Apr 10, 2019
Same also applies to child processes, fwiw: #2333.
pi0 commentedon May 7, 2019
@silverwind For
child_process(cluster and fork) it is possible to pass current process stdio using the third argument ({ stdout: process.stdout }) and it preservesisTTYbut for threads, we can't.As a (temporary) workaround, we can set
workerData: { isTTY: process.stdout.isTTY }, when creatingWorkerand in very beginning of worker script:bnoordhuis commentedon Jan 21, 2020
I'm going to close this out. It's working as expected as far as I'm concerned and 343ddff adds hints to the documentation.
sindresorhus commentedon Nov 24, 2020
@bnoordhuis It would be useful to be able to know whether the main thread is TTY though. When you know you don't need things that are unsupported. For example, it could be useful to decide whether to print ANSI colors from the worker thread. Could Node.js somehow expose to the worker thread whether stdout/stderr in the main thread is TTY?
LinqLover commentedon Jan 24, 2025
IMHO
process.stdout.isTTYshould never beundefinedbut simplyfalse.Fix colorize error when stderr isn't TTY