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?

6 Answers 6

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);
    }
});
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.

5
  • 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. Sep 8, 2012 at 10:23
  • Thanks, that makes perfect sense
    – Fluffy
    Sep 8, 2012 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 9, 2014 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, 2016 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. Sep 29, 2016 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);
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 [email protected]

1
  • 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.
    – Community Bot
    Jan 20, 2022 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.

-1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.