'How to handle Array buffer allocation failed in nodejs?

I have successfully upload image in nodejs and I am using multer for that. But sometime " Array buffer allocation failed " error occurs and I want to handle that error. I tried to keep the code in try-catch block even though still it throws the error and I am unable to figure out how to handle this error or how to avoid that error ? Able to upload file but when I try to change height and width and making a new image then it throws an error

Uncaught RangeError: Array buffer allocation failed

 at Buffer.Uint8Array (native)

 at FastBuffer (buffer.js:8:1)

OR

 Array buffer allocation failed

   at Buffer.Uint8Array (native)

   at FastBuffer (buffer.js:8:1)

   RangeError: Array buffer allocation failed
    at new Uint8Array (native)
    at Object.getData (/Burg_nodejs/node_modules/jpeg-js/lib/decoder.js:797:18)
    at Object.copyToImageData (/Burg_nodejs/node_modules/jpeg-js/lib/decoder.js:913:23)
    at Object.decode (/Burg_nodejs/node_modules/jpeg-js/lib/decoder.js:985:11)
    at Jimp.parseBitmap (/Burg_nodejs/node_modules/jimp/index.js:310:36)
    at /Burg_nodejs/node_modules/jimp/index.js:202:29
    at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:446:3)

Any suggestion will be much more helpful



Solution 1:[1]

Looks like you do not have enough memory to perform a large size upload

Solution 2:[2]

jpeg-js uses inadequate amount of memory for image processing,

- possible solution could be to resize picture (imagemagick/whatever)

Solution 3:[3]

Someone in our team had this issue, so we guessed something was off with his machine. Turns out he had node x86 installed. After installing node x64 and rebuilding all the node_modules, the error was gone.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Amir
Solution 2 IvanM
Solution 3 JJP