title: Unable to upload large files using nodejs/axios
body: <p>I am writing a nodejs client that would upload files (files can be both binary or text files) from my local dev machine to my server which is written in Java, configuring which is not an option. I am using the following code to upload files, it works fine for files upto 2 gb, but beyond that it throws an error mentioned below. Now you may think that the server might not be allowing files more than 2 gb but I have successfully uploaded files upto 10 gb using Rest clients like Postman and Insomnia on the same instance.</p>
  <pre><code>const fs = require(&quot;fs&quot;);
  const path = require(&quot;path&quot;);
  const axios = require(&quot;axios&quot;);
  const FormData = require(&quot;form-data&quot;);
  
  function uploadAxios({ filePath }) {
    
  let formData;
  try {
    formData = new FormData();
    formData.append(&quot;filedata&quot;, fs.createReadStream(filePath));
  } catch (e) {
    console.error(e)
  }
  
    axios
      .post(
        `https://myinstance.com`,
        formData,
        {
          headers: {
            ...formData.getHeaders(),
            &quot;Content-Type&quot;: &quot;multipart/form-data&quot;,
            Authorization:
              &quot;Basic xyz==&quot;,
          },
          maxContentLength: Infinity,
          maxBodyLength: Infinity,
          // maxContentLength: 21474836480,
          // maxBodyLength: 21474836480, // I have tried setting these values with both numbers and the keyword Infinity but nothing works
        }
      )
      .then(console.log)
      .catch(console.error);
  }
  
  const filePath = &quot;C:\\Users\\phantom007\\Documents\\BigFiles\\3gb.txt&quot;;
   
  uploadAxios({  filePath  });
  </code></pre>
  <p>Error I get:</p>
  <pre><code>#
  # Fatal error in , line 0
  # API fatal error handler returned after process out of memory
  #
  
  &lt;--- Last few GCs ---&gt;
  es[7844:0000023DC49CE190]    47061 ms: Mark-sweep 33.8 (41.8) -&gt; 33.8 (41.8) MB, 417.2 / 0.1 ms  (+ 947.1 ms in 34029 steps since start of marking, biggest step 431.0 ms, walltime since start of marking 15184 ms) finalize incremental marking via stack guard[7844:0000023D
  C49CE190]    48358 ms: Mark-sweep 34.4 (41.8) -&gt; 31.8 (40.5) MB, 1048.4 / 0.0 ms  (+ 0.0 ms in 1 steps since start of marking, biggest step 0.0 ms, walltime since start of marking 1049 ms) finalize incremental marking via task GC in old spac
  
  &lt;--- JS stacktrace ---&gt;
  
  ==== JS stack trace =========================================
  
  Security context: 000002E294C255E9 &lt;JSObject&gt;
      0: builtin exit frame: new ArrayBuffer(aka ArrayBuffer)(this=0000022FFFF822D1 &lt;undefined&gt;,65536)
  
      1: _read [fs.js:~2078] [pc=0000004AD942D301](this=0000039E67337641 &lt;ReadStream map = 000002F26D804989&gt;,n=65536)
      2: read [_stream_readable.js:454] [bytecode=000002A16EB59689 offset=357](this=0000039E67337641 &lt;ReadStream map = 000002F26D804989&gt;,n=0)
      3: push [_stream_readable.js:~201]...
  
  FATAL ERROR: Committing semi space failed. Allocation failed - process out of memory
  </code></pre>
  <p>It looks like the error is because it has exceed the memory limit, i know by passing the flag --max-old-space-size i can overcome this, but i want this to be scalable and not hardcode an upper limit.</p>
  <p>PS: My dev machine has 12 GB free memory</p>
  <p>Edit: I added the error trace.</p>