0
$ cat main.js 
#!/usr/bin/env node
// vim: set noexpandtab tabstop=2:

fs=require('fs');
//data=fs.readFileSync(process.stdin.fd);
data=fs.readFileSync('/dev/stdin');
console.log(data.toString());
$ ./main.js <<< 'Hello World!'
Hello World!

It seems that both '/dev/stdin' and process.stdin.fd works the same at least in my environment (Mac OS X). Are they always work the same on Unix variant systems? Thanks.

user1424739
  • 11,937
  • 17
  • 63
  • 152

1 Answers1

2

According to the documentation, process.stdin.fd can point either to a net.Socket or a file when fd0 refers to it. In the first case it's a Duplex stream, in the second it's a Readable stream. On the other hand, fd0 still technically is stdin as explained here in both cases. Node had some issues regarding this functionality on OS X, you can read about them here.

I think you can assume that /dev/stdin and process.stdin.fd are interchangeable on Unix variant systems, but I would prefer to use /dev/stdin because process.stdin.fd has been used mainly for debugging purposes in Node.

Tomasz Kasperczyk
  • 1,991
  • 3
  • 22
  • 43