I’m having problems with getting the error:
events.js:48
throw arguments[1]; // Unhandled 'error' event
^
Error: write EPIPE
at errnoException (net.js:670:11)
at Object.afterWrite [as oncomplete] (net.js:503:19)
when piping output to head. A simple case to try it out is:
console.log('some string');
... the same for 20 lines
and then node test.js | head to get the error, which seems to appear in about 70% runs on Ubuntu 12.04. What’s the problem?
The
headcommand only reads the first few lines. Your code expects all of its output to be read and triggers an error if it cannot produce output. If it is legal to throw away output from your program, don’t treat it as a fatal error in the program. If it’s not legal to throw away output from your program, don’t pipe it tohead.You currently have a race condition. If
headbegins ignoring input before the program finishes writing its output, the program gets an exception. If the program finishes writing its output beforeheadbegins ignoring its input, everything is fine.As a silly temporary fix:
node test.js | tee /dev/null | headNow,
teewill take all the program’s output.