I'm currently writing a simple CLI with Node.js 6.1.0. Here's an example of how I'm trying to read from stdin:
'use strict';
const fs = require('fs');
const BUFFER_LENGTH = 8;
const buffer = Buffer.alloc(BUFFER_LENGTH);
fs.readSync(process.stdin.fd, buffer, 0, BUFFER_LENGTH);
console.log(buffer.toString());
When running this on OS X Yosemite (10.10.5), node exits with Error: EAGAIN: resource temporarily unavailable, read. Interestingly, it works if I replace process.stdin.fd with fs.openSync('/dev/stdin', 'rs').
Another point of interest is that I can use process.stdin.fd with Node.js 4.4.0 as long as I replace Buffer.alloc with a call to the deprecated Buffer constructor via new.
Finally, process.stdin.fd and fs.openSync('/dev/stdin', 'rs') don't return the same FD Number; on my system, the return values are 0 and 14 respectively.
Am | missing something obvious or is there a potentially bug with process.stdin.fd on OS X/Node.js 6?
Thanks for your time,
James
There is a long and technical explanation but the summary is that, on OS X, process.stdin.fd is not the real tty file descriptor because of a kernel bug workaround.
Aside: the .fd property is not intended to be used like that. It is undocumented and mainly exists for debugging purposes.
I'll close the issue as working-as-intended but let me know if you have more questions.
Aah, I didn't realise it was an undocumented API! Thanks for clearing that up!
I get the same error on Linux, not just on Mac, is there no way to directly read from STDIN fd, without going through streams?
@CMCDragonkai You can fs.open('/dev/tty') and read from that file descriptor. You'll have to make sure nothing in your program uses process.stdin though or you'll get mixed results.
@bnoordhuis What kind of mixed results are you implying?
I figured to use process.stdin.fd on Windows, but on Linux/Mac to open /dev/fd/0 instead.
@CMCDragonkai With one resource and two readers it's indeterminate who reads what: reader A may read the first line, B the second line, A the third line, etc.
In my case, that's the expected behaviour. Thanks!
Aside: the
.fdproperty is not intended to be used like that. It is undocumented and mainly exists for debugging purposes.
It is currently documented:
https://nodejs.org/api/process.html#process_process_stdin_fd
Aside: the
.fdproperty is not intended to be used like that. It is undocumented and mainly exists for debugging purposes.It is actually documented:
https://nodejs.org/api/process.html#process_process_stdin_fd
I don't think it was documented when I raised the issue, but it's worth noting that a lot has changed since version 6.
Most helpful comment
There is a long and technical explanation but the summary is that, on OS X,
process.stdin.fdis not the real tty file descriptor because of a kernel bug workaround.Aside: the
.fdproperty is not intended to be used like that. It is undocumented and mainly exists for debugging purposes.I'll close the issue as working-as-intended but let me know if you have more questions.