Jhon D Jhon D - 1 month ago 11
Javascript Question

Using multer cause error when running app

Im having node application with express which use multer module

in the

file I put the following:

var mulStorage = require("./utils/store"),
var upload = multer({
storage: mul.storage,
dest: 'uploads/'

file look like following

var multer = require('multer');

var stor = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './uploads/')
filename: function (req, file, cb) {
var filename = file.originalname;
var fileExtension = filename.split(".")[1];
cb(null, Date.now() + "." + fileExtension);
module.exports = {
stor: stor

When I run request using postman I got the following error:

Error: ENOENT: no such file or directory, open

   at Error (native)

Why multer doesn't create the folder if it doesn't exist??

If I'm creating the upload folder under the root manually this is working...

When I change the following and remove the
storage: mul.storage,

This is working, but I need the storage to determine the file name ...

var upload = multer({
//storage: mul.storage,
dest: 'uploads/'

Even If I remove the property dest from multer object and keep only the storage I got the same error...


Why multer doesn't create the folder if it doesn't exist??

This is in documentation (link) :

Note: You are responsible for creating the directory when providing destination as a function. When passing a string, multer will make sure that the directory is created for you.

I don't know why the author made this decision, but as you see this is not a bug.

You can use fs module to create directories.