Search code examples
node.jsmulter

Upload large file (>2GB) with multer


I'm trying to upload a large file (7GB) to my server. For this I'm using multer:

const express = require('express');

const multer = require('multer');

const {
    saveLogFile,
} = require('../controller/log');

const router = express.Router();
const upload = multer();

router.post('/', upload.single('file'), saveLogFile);

In my saveLogFile controller, which is of format saveLogFile = async (req,res) => { ... } I want to get req.file. The multer package should give me the uploaded file with req.file. So when I try to upload small files (<2GB) It goes successfully. But when I try to upload files over 2GB, I get the following error:

buffer.js:364
    throw new ERR_INVALID_OPT_VALUE.RangeError('size', size);
    ^

RangeError [ERR_INVALID_OPT_VALUE]: The value "7229116782" is invalid for option "size"

How can I bypass it? Actually, All I need is access for the uploaded file in my saveLogFile Controller.


Solution

  • The reason for this is probably that node will run out of memory as your using multer without passing any options. From the docs:

    In case you omit the options object, the files will be kept in memory and never written to disk.

    Try using the dest or storage option in order to use a temporary file for the upload:

    const upload = multer({ dest: './some-upload-folder' });
    router.post('/', upload.single('file'), saveLogFile);