Probably a basic question for those familiar with the topic. Consider the following toy program:
const fs = require('fs');
process.stdout.on('data', (chunk) => {
fs.writeFileSync('myfile.txt', chunk, 'utf-8'); // just an example
});
process.stdout.write('xyz');
If I run this code just as it is, I get the following error:
errno: -4053,
code: 'ENOTCONN',
syscall: 'read'
I already do not understand why that happens. But it gets even stranger:
When I run the code with a console.log()
before it, no error is thrown anymore! However, the listener I defined for the data
event seems not to be executed in that case, as no text file is created.
Can someone explain to me why this happens and what I can do get the expected result (here write to myfile.txt)?