multer: ENOENT error when deploy to heroku

the error:

2018-05-12T05:47:21.500393+00:00 app[web.1]: HTTP request POST /api/postImage/ {}
2018-05-12T05:47:21.501779+00:00 app[web.1]: HTTP request undefined POST /api/postImage/ {}
2018-05-12T05:47:21.591203+00:00 app[web.1]: { Error: ENOENT: no such file or directory, open '/app/static/uploads/76cc729da26dc8da18cda7924fc5eaf31526104041562.png'
2018-05-12T05:47:21.591209+00:00 app[web.1]: code: 'ENOENT',
2018-05-12T05:47:21.591213+00:00 app[web.1]: syscall: 'open',
2018-05-12T05:47:21.591216+00:00 app[web.1]: path: '/app/static/uploads/76cc729da26dc8da18cda7924fc5eaf31526104041562.png',
2018-05-12T05:47:21.591217+00:00 app[web.1]: storageErrors: [] }
2018-05-12T05:47:21.591207+00:00 app[web.1]: errno: -2,

multer setup:

var multer = require('multer');
var storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, path.join(__dirname, '/static/uploads/'))
        // cb(null, "localhost:" + process.env.PORT + '/static/uploads/')
    },
    filename: function (req, file, cb) {
        crypto.pseudoRandomBytes(16, function (err, raw) {
            cb(null, raw.toString('hex') + Date.now() + '.' + mime.extension(file.mimetype));
        });
    }
});
var upload = multer({storage: storage});

and port is set to: var PORT = process.env.PORT || 3000; project structure: screen shot 2018-05-12 at 01 48 29

what did i mess up?

Thanks in advance!

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 2
  • Comments: 18 (4 by maintainers)

Most upvoted comments

I was having the same issue after deploying my Mean Stack app to heroku. I was pushing the code with empty uploads folder. After adding a temporary file in uploads folder , Issue is gone.

It seems like the /app/static/uploads/ directory doesn’t exist. Might it be that the Heroku client doesn’t upload empty folders? Or that it’s ignored in some .*ignore file?

I would suggest creating the directory when the process starts for the first time:

try {
  fs.mkdirSync(path.join(__dirname, '/static/uploads/'))
} catch (err) {
  if (err.code !== 'EEXIST') throw err
}

Seems like the problem is that Heroku strips empty folders. As mentioned, placing an empty file in the upload folder works around that 👍