How to detect if Node's process.stdout is being piped?

simme

Is there any way I can detect if the output from my Node.js script is being piped to something other then the terminal?

I would like some way of detecting if this is happening:

node myscript.js | less

Or if this is happening:

node myscript.js

Benjamin Gruenbaum

The easiest way would be process.stdout.isTTY (0.8 +):

$ node -p -e "Boolean(process.stdout.isTTY)"
true
$ node -p -e "Boolean(process.stdout.isTTY)" | cat
false

(example from the official documentation)

Alternatively you can use the tty module for finer grained control:

if (require('tty').isatty(1)) {
    // terminal
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

stdout of spawned process works strange when being piped

From Dev

stdout of spawned process works strange when being piped

From Dev

How does a Unix program know to change output if it's being piped?

From Dev

Send find's output to stdout and piped to grep

From Dev

How is stdout piped in to a file located in a different directory?

From Dev

How to detect if a Node spawned process is still running?

From Dev

How to detect if python script is being run as a background process

From Dev

How can a process detect that it's in a child namespace?

From Dev

How to see stdout of a phantomjs child process using node.js?

From Dev

How do I print a buffer to stdout piped through a pager?

From Dev

How to output piped stdout in bash script from getopts?

From Dev

How to capture stdout and sterr AND proper piped exit status in Shell command?

From Dev

How does a program output to a terminal when stdout is piped?

From Dev

node.js STDIN works with process.stdin, not when piped

From Dev

How to detect process's highest cpu usage during it's life

From Dev

Dump process's stdin and stdout

From Dev

How to tell if my program is being piped to another (Perl)

From Dev

How can I clear/empty a file being piped to?

From Dev

Clojure: how to tell if out is going to console or is being piped?

From Dev

vimscript detect piped input

From Dev

Does powershell let you detect if output is being directed to the screen, vs piped to variable/file?

From Dev

Does powershell let you detect if output is being directed to the screen, vs piped to variable/file?

From Dev

How to detect if sprite is being touched?

From Dev

How to detect if sprite is being touched?

From Dev

Is a command's stdout to the terminal being logged?

From Dev

How to discard stdout of child process but keep stdout of parent process?

From Dev

How to pass messages as well as stdout from child to parent in node.js child process module?

From Dev

Ruby pipe from child's stdout not closing/how to detect empty pipe

From Dev

How do I pass a buffer as a piped input to a node script?

Related Related

  1. 1

    stdout of spawned process works strange when being piped

  2. 2

    stdout of spawned process works strange when being piped

  3. 3

    How does a Unix program know to change output if it's being piped?

  4. 4

    Send find's output to stdout and piped to grep

  5. 5

    How is stdout piped in to a file located in a different directory?

  6. 6

    How to detect if a Node spawned process is still running?

  7. 7

    How to detect if python script is being run as a background process

  8. 8

    How can a process detect that it's in a child namespace?

  9. 9

    How to see stdout of a phantomjs child process using node.js?

  10. 10

    How do I print a buffer to stdout piped through a pager?

  11. 11

    How to output piped stdout in bash script from getopts?

  12. 12

    How to capture stdout and sterr AND proper piped exit status in Shell command?

  13. 13

    How does a program output to a terminal when stdout is piped?

  14. 14

    node.js STDIN works with process.stdin, not when piped

  15. 15

    How to detect process's highest cpu usage during it's life

  16. 16

    Dump process's stdin and stdout

  17. 17

    How to tell if my program is being piped to another (Perl)

  18. 18

    How can I clear/empty a file being piped to?

  19. 19

    Clojure: how to tell if out is going to console or is being piped?

  20. 20

    vimscript detect piped input

  21. 21

    Does powershell let you detect if output is being directed to the screen, vs piped to variable/file?

  22. 22

    Does powershell let you detect if output is being directed to the screen, vs piped to variable/file?

  23. 23

    How to detect if sprite is being touched?

  24. 24

    How to detect if sprite is being touched?

  25. 25

    Is a command's stdout to the terminal being logged?

  26. 26

    How to discard stdout of child process but keep stdout of parent process?

  27. 27

    How to pass messages as well as stdout from child to parent in node.js child process module?

  28. 28

    Ruby pipe from child's stdout not closing/how to detect empty pipe

  29. 29

    How do I pass a buffer as a piped input to a node script?

HotTag

Archive