2

While trying to read the file size is 5GB in node.js I got this error:

error: RangeError [ERR_FS_FILE_TOO_LARGE]: File size (6003804160) is greater than possible Buffer: 2147483647 bytes

fs.readFile(tempfile, "utf8", function(err, filebuffer) {
      console.log(err,"filebuffer " ,filebuffer);
})

Please suggest the solution.

1 Answer 1

4

2147483647 bytes is the maximum size a Buffer can have in NodeJS. Note that this operation will load all the contents of the file into memory at once. Are you sure that you want to push 5gb of data into your memory?

If you need to read files larger than that, you will need to use fs.createReadStream function instead of fs.readFile. fs.createReadStream allow you to "split" the file into small(er) chunks and read one chunk at a time.

1
  • hi, thanks for suggestion, when i use const readStream = fs.createReadStream('SW_DVD9_Win_Server_STD_CORE_2016_64Bit_English_-4_DC_STD_MLF_X21-70526.ISO',{ highWaterMark : 5 * 1024 * 1024}); and during this operation i get [Error: EIO: i/o error, read] { errno: -4070, code: 'EIO', syscall: 'read' } and complete file also not read. please suggest some solutions. Aug 10, 2020 at 12:36

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.