Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't read from process.stdin.fd in Node.js 6.10 #7439

Closed
jamesseanwright opened this issue Jun 27, 2016 · 9 comments
Closed

Can't read from process.stdin.fd in Node.js 6.10 #7439

jamesseanwright opened this issue Jun 27, 2016 · 9 comments

Comments

@jamesseanwright
Copy link

jamesseanwright commented Jun 27, 2016

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

@bnoordhuis
Copy link
Member

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.

@jamesseanwright
Copy link
Author

Aah, I didn't realise it was an undocumented API! Thanks for clearing that up!

@CMCDragonkai
Copy link

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?

@bnoordhuis
Copy link
Member

@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.

@CMCDragonkai
Copy link

@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.

@bnoordhuis
Copy link
Member

@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.

@CMCDragonkai
Copy link

In my case, that's the expected behaviour. Thanks!

@rolfen
Copy link

rolfen commented Oct 12, 2020

Aside: the .fd property 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

@jamesseanwright
Copy link
Author

jamesseanwright commented Oct 17, 2020

Aside: the .fd property 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants