Skip to content

process.stdout/stderr.isTTY undefined in worker_thread #26946

Closed

Description

@mdouglass
  • Version: v11.12.0
  • Platform: macOS Mojave 10.14.4 (Darwin brewster.local 18.5.0 Darwin Kernel Version 18.5.0: Mon Mar 11 20:40:32 PDT 2019; root:xnu-4903.251.3~3/RELEASE_X86_64 x86_64)
  • Subsystem: worker_threads

In worker threads, process.stdout.isTTY and process.stderr.isTTY are undefined.

const { Worker, isMainThread } = require('worker_threads')
if (isMainThread) {
  console.log('mainThread, process.stdout.isTTY', process.stdout.isTTY)
  console.log('mainThread, process.stderr.isTTY', process.stderr.isTTY)
  new Worker(__filename)
} else {
  console.log('workerThread, process.stdout.isTTY', process.stdout.isTTY)
  console.log('workerThread, process.stderr.isTTY', process.stderr.isTTY)
}
$ node ./main.js 
mainThread, process.stdout.isTTY true
mainThread, process.stderr.isTTY true
workerThread, process.stdout.isTTY undefined
workerThread, process.stderr.isTTY undefined

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.

Activity

  1. added
    workerIssues and PRs related to the worker_threads module and Worker API.
    on Mar 27, 2019
  2. BridgeAR commented on Mar 27, 2019

    @BridgeAR
    Member

    // cc @addaleax

  3. bnoordhuis commented on Mar 28, 2019

    @bnoordhuis
    Member

    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.

  4. silverwind commented on Apr 10, 2019

    @silverwind
    Contributor

    Same also applies to child processes, fwiw: #2333.

  5. pi0 commented on May 7, 2019

    @pi0

    @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:

    const { workerData } = require('worker_threads')
    
    if (workerData.isTTY) {
      for (const stream of ['stdout', 'stdin', 'stderr']) {
        if (process[stream].isTTY === undefined) {
          process[stream].isTTY = true
        }
      }
    }
  6. bnoordhuis commented on Jan 21, 2020

    @bnoordhuis
    Member

    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.

  7. sindresorhus commented on Nov 24, 2020

    @sindresorhus

    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?

  8. LinqLover commented on Jan 24, 2025

    @LinqLover

    IMHO process.stdout.isTTY should never be undefined but simply false.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    workerIssues and PRs related to the worker_threads module and Worker API.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions