Skip to content
Advertisement

Detect if node receives stdin

I’m interested in determining if my node script is being called with data being streamed into it or not. That is, I want to differentiate between these two cases

$ node index.js
$ ls | node index.js

Advertisement

Answer

process.stdin.isTTY will be false when you have data piped to stdin:

$ node -p -e "Boolean(process.stdin.isTTY)"
true
$ ls | node -p -e "Boolean(process.stdin.isTTY)"
false

See docs: https://nodejs.org/api/tty.html

Advertisement