question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

multer 2.0.0 buffer is undefined

See original GitHub issue

Due to a security vulnerability, I have to upgrade to 2.0.0+ but now req.file.buffer is undefined. memoryStorage() is no longer a function, so can you help me figure out how to get the buffer object for my file?

const multer = require('multer');
const storage = multer.memoryStorage() // memoryStorage() is not a function
const upload = multer({ storage: storage })

router.post('/', upload.single('file'), function (req, res, next) {
    console.log('file:', req.file) // ok
    console.log('path:', req.file.path) // ok
    console.log('buffer:', req.file.buffer) // undefined when I don't use memoryStorage
}

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
LinusUcommented, Jan 29, 2019

Due to a security vulnerability, I have to upgrade to 2.0.0+

Could you elaborate, would love to fix this in 1.x

memoryStorage() is no longer a function

That’s correct, we’ve moved to using streams instead of having specific storage engines. To read it into memory, use e.g. get-stream:

const multer = require('multer')
const getStream = require('get-stream')

const upload = multer()

router.post('/', upload.single('file'), async function (req, res, next) {
    console.log('file:', req.file) // ok
    const buffer = await getStream(req.file.stream)
}
0reactions
LinusUcommented, May 20, 2020

@RegularDan can’t you send the stream directly to imgur, instead of using getStream to turn it into a buffer? Something like got.post('https://imgur.com/api/upload', { body: req.file.stream })?

Anyhow, since the original question have been answered I will close this…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Multer file buffer missing - node.js - Stack Overflow
So when I try to access req.file.buffer it returns undefined. Can someone help to explain. ` const multer = require('multer') ...
Read more >
How to upload files in NodeJS using Multer 2.0! - picnature
With Multer 2.0.0, it has become easier than ever to handle uploading logic in your route function. For instance, we already discussed the...
Read more >
multer - npm
Start using multer in your project by running `npm i multer`. There are 3538 other projects in the npm registry using multer.
Read more >
Express multer middleware
When using memory storage, the file info will contain a field called buffer that contains the entire file. WARNING: Uploading very large files,...
Read more >
req.file.path undefined multer - You.com | The AI Search ...
Req.file Is Undefined Multer When working with the Multer library in Node.js, you may encounter the “req.file is undefined” error. This can ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found