35

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?

Fluffy
  • 27,504
  • 41
  • 151
  • 234

6 Answers6

27

To alter the program to exit successfully in the case of a closed pipe, try:

process.stdout.on('error', function( err ) {
    if (err.code == "EPIPE") {
        process.exit(0);
    }
});
Jim
  • 359
  • 3
  • 2
22

The head command 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 to head.

You currently have a race condition. If head begins ignoring input before the program finishes writing its output, the program gets an exception. If the program finishes writing its output before head begins ignoring its input, everything is fine.

As a silly temporary fix: node test.js | tee /dev/null | head
Now, tee will take all the program's output.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • 3
    See my update to the answer. It's a race condition. You have two things that are totally unsynchronized (`head` closing its input and `test.js` finishing its output) with behavior that varies based on which happens first. – David Schwartz Sep 08 '12 at 10:23
  • Thanks, that makes perfect sense – Fluffy Sep 08 '12 at 10:34
  • Is this common in scripting langs? Is console.log asynchronous in Node and does that have anything to do with this? – Steve Clay Feb 09 '14 at 14:20
  • @DavidSchwartz regarding "Your code expects all of its output to be read and triggers an error if it cannot produce output" - why is it the program's responsibility to discern how others read its output? Meaning, why *should* an error occur? Seems to me as long as the program outputted all its information correctly, then how others read it is exclusively their concern. – aaaaaa Sep 29 '16 at 17:35
  • 1
    @aaaaaa A programmer could make that choice and ignore all errors relating to writing output. But this particular program didn't make that choice. This program treats an inability to write output as a fatal error. – David Schwartz Sep 29 '16 at 17:38
2

Was piping to a child process which managed to crash main process and fixed my issue by adding a stdin error callaback:

const p = child.spawn(myCommand);

// this just prevents exiting main node process and exits process "p" instead
p.stdin.on('error', function () {});

stream.pipe(p.stdin);
user5480949
  • 1,410
  • 1
  • 15
  • 22
0

its work for me, I was trying to upload my code to aws ec2 but I face this error (Error: write EPIPE) (npm uninstall phantomjs-prebuilt) and install again (npm install phantomjs-prebuilt@2.1.13

Amir Khan
  • 1
  • 3
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 20 '22 at 17:30
0

The solution for me was to add the domain that I was using for my project to the host file of my system.

You can find this domain in files like: .env, config.babel.js among others.

Juanma Menendez
  • 17,253
  • 7
  • 59
  • 56
-1

Following command worked for me: sudo apt-get install libfontconfig