title: EPIPE Error when uploading large file using nodejs
body: <p>I was following <a href="https://pagep.net/2018/03/31/how-to-handle-large-file-upload-with-nodejs-express-server/" rel="nofollow noreferrer">this</a> article to setup a nodejs server on my local machine (which has 16 gb memory and about 170gb free disk-space) and uploaded a 20 gb file, for the first couple of times the file got uploaded successfully, but after a while i started getting EPIPE error:</p>
  <pre><code>error FetchError: request to http://localhost:3200/upload failed, reason: write EPIPE
      at ClientRequest.&lt;anonymous&gt; (/Volumes/FreeAgent GoFlex Drive/Test/multer-project/node_modules/node-fetch/lib/index.js:1455:11)
      at ClientRequest.emit (events.js:327:22)
      at Socket.socketErrorListener (_http_client.js:467:9)
      at Socket.emit (events.js:315:20)
      at emitErrorNT (internal/streams/destroy.js:100:8)
      at emitErrorCloseNT (internal/streams/destroy.js:68:3)
      at processTicksAndRejections (internal/process/task_queues.js:84:21) {
    type: 'system',
    errno: 'EPIPE',
    code: 'EPIPE'
  }
  </code></pre>
  <p>When i checked, the file got uploaded partially and was about 28mb in size. I tried uploading the file from both Postman, browser and a nodejs script, but got the same EPIPE error message. I am not sure why is this happening, googling the error message didn't help. I am not sure how to overcome this. Following is my server and client code.</p>
  <pre><code>// server.js
  const express = require(&quot;express&quot;); // Express Web Server
  const busboy = require(&quot;connect-busboy&quot;); // Middleware to handle the file upload https://github.com/mscdex/connect-busboy
  const path = require(&quot;path&quot;); // Used for manipulation with path
  const fs = require(&quot;fs-extra&quot;);  
  
  const app = express(); // Initialize the express web server
  app.use(
    busboy({
      highWaterMark: 2 * 1024 * 1024 // Set 2MiB buffer
    })
  ); // Insert the busboy middle-ware
  
  const uploadPath = path.join(__dirname, &quot;uploads/&quot;); // Register the upload path
  fs.ensureDir(uploadPath); // Make sure that he upload path exits
  
  /**
   * Create route /upload which handles the post request
   */
  app.route(&quot;/upload&quot;).post((req, res, next) =&gt; {
    req.pipe(req.busboy); // Pipe it trough busboy
  
    req.busboy.on(&quot;file&quot;, (fieldname, file, filename) =&gt; {
      console.log(`Upload of '${filename}' started`);
  
      // Create a write stream of the new file
      const fstream = fs.createWriteStream(path.join(uploadPath, filename));
      // Pipe it trough
      file.pipe(fstream);
  
      // On finish of the upload
      fstream.on(&quot;close&quot;, () =&gt; {
        console.log(`Upload of '${filename}' finished`);
        res.send(&quot;ok&quot;);
      });
    });
  });
  
  /**
   * Serve the basic index.html with upload form
   */
  app.route(&quot;/&quot;).get((req, res) =&gt; {
    res.writeHead(200, { &quot;Content-Type&quot;: &quot;text/html&quot; });
    res.write(
      '&lt;form action=&quot;upload&quot; method=&quot;post&quot; enctype=&quot;multipart/form-data&quot;&gt;'
    );
    res.write('&lt;input type=&quot;file&quot; name=&quot;fileToUpload&quot;&gt;&lt;br&gt;');
    res.write('&lt;input type=&quot;submit&quot;&gt;');
    res.write(&quot;&lt;/form&gt;&quot;);
    return res.end();
  });
  
  const server = app.listen(3200, function() {
    console.log(`Listening on port ${server.address().port}`);
  });
  </code></pre>
  <p>and my client code is:</p>
  <pre><code>// client.js
  const fs = require(&quot;fs&quot;);
  const FormData = require(&quot;form-data&quot;);
  const fetch = require(&quot;node-fetch&quot;);
  
  var formdata = new FormData();
  formdata.append(
    &quot;file&quot;,
    fs.createReadStream(&quot;/Users/phantom007/My Documents/35gb.myfile&quot;)
  );
  
  var requestOptions = {
    method: &quot;POST&quot;,
    body: formdata,
    redirect: &quot;follow&quot;
  };
  
  fetch(&quot;http://localhost:3200/upload&quot;, requestOptions)
    .then(response =&gt; response.text())
    .then(result =&gt; console.log(result))
    .catch(error =&gt; console.log(&quot;error&quot;, error));
  </code></pre>