JuanCaicedo
JuanCaicedo

Reputation: 3428

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

Upvotes: 15

Views: 2133

Answers (1)

JuanCaicedo
JuanCaicedo

Reputation: 3428

process.stdin.isTTY will be falsy (undefined) when you have data piped to stdin and true when you don't:

$ node -e "console.log(process.stdin.isTTY)"
true
$ ls | node -e "console.log(process.stdin.isTTY)"
undefined

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

Upvotes: 11

Related Questions