Languages

Node.js - Error: EAGAIN: resource temporarily unavailable, read

7 points
Asked by:
DEX7RA
580

Any idea why Node.js throws exception when I try to read process.stdin using fs.readSync(fd, buffer, offset, length)?

I thought, if I will pipe input stream it should be working well.

I try to run Node.js as sub-process.

Under Windows all warks great, under Linux I get:

Error: EAGAIN: resource temporarily unavailable, read
    at Object.readSync (node:fs:749:3)
    at exports.readStream (/opt/shop/utils.js:19:19)
    at async /opt/shop/shop/index.js:13:5

What it menas? Is my pipe broken?

1 answer
3 points
Answered by:
DEX7RA
580

EAGAIN code just means: there is nothing to read now and you should try read later.

Example unification for Window and Unix/Linux:

const fs = require('fs');

// Reads bytes into buffer returning -1 when transmission ended.
//
const readSync = (fd, buffer, offset, length) => {
    try {
        const result = fs.readSync(fd, buffer, offset, length);
        return result === 0 ? -1 : result;
    } catch (error) {
        if (error.code === 'EAGAIN') {  // when there is nothing to read at the current time 
            return 0;
        }
        throw error;
    }
};

 

See also

  1. Node.js - read process.stdin using fs.readSync() function (sync reading)

Refrences

  1. https://nodejs.org/api/os.html#posix-error-constants
0 comments Add comment
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join