Skip to content

worker_threads in node.js report supportsColor false #97

@mdouglass

Description

@mdouglass

In worker threads, supports-color is unable to automatically detect color support.

const { Worker, isMainThread } = require('worker_threads')
const supportsColor = require('supports-color')

if (isMainThread) {
  console.log('mainThread, supportsColor.stdout', supportsColor.stdout)
  console.log('mainThread, supportsColor.stderr', supportsColor.stderr)
  new Worker(__filename)
} else {
  console.log('workerThread, supportsColor.stdout', supportsColor.stdout)
  console.log('workerThread, supportsColor.stderr', supportsColor.stderr)
}
$ node ./main.js 
mainThread, supportsColor.stdout { level: 2, hasBasic: true, has256: true, has16m: false }
mainThread, supportsColor.stderr { level: 2, hasBasic: true, has256: true, has16m: false }
workerThread, supportsColor.stdout false
workerThread, supportsColor.stderr false

It looks like the stream type in worker threads is WritableWorkerStdio which returns isTTY false so the check at:

if (stream && !stream.isTTY && forceColor === undefined) {
fails. If I skip that check (with stream.constructor.name === 'WritableWorkerStdio') then supportsColor returns correct information and color output does work. I'm not sure if that's the best fix or whether it even should be fixed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions