Multer: ENOENT error when deploy to heroku

Created on 12 May 2018  Â·  15Comments  Â·  Source: expressjs/multer

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!

Most helpful comment

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
}

All 15 comments

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
}

@LinusU
my code
const storage = multer.diskStorage({
destination : function(req,file,cb){
cb(null,'./public/gambar/')

},
filename : function(req,file,cb){
cb(null,file.fieldname + '-' + Date.now() + '.jpg')
}
})
after im upload via application
this path
http://batikmart.herokuapp.com/dashboard/product/uploads/flPhoto-1540382362434.jpg
not same with destination

so image crash and doesnt upload to path destination

@LinusU
in my app.js im set app.use(express.static(path.join(__dirname, 'public')));

@kukode does the folder gambar exist?

Yes

On Mon, Oct 29, 2018, 6:02 PM Linus Unnebäck notifications@github.com
wrote:

@kukode https://github.com/kukode does the folder gambar exist?

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/expressjs/multer/issues/591#issuecomment-433870128,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AhicUcmFj38YqyHhCpKziPLf9b0JjFC_ks5upuBjgaJpZM4T8Sjm
.

Could you change './public/gambar/' to something like this?

path.join(__dirname, 'public/gambar')

Yes i change to

On Mon, Oct 29, 2018, 6:06 PM Linus Unnebäck notifications@github.com
wrote:

Could you change './public/gambar/' to something like this?

path.join(__dirname, 'public/gambar')

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/expressjs/multer/issues/591#issuecomment-433871078,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AhicUUIkNRcgD4TSg5-EjPaVyVyb78ABks5upuEugaJpZM4T8Sjm
.

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.

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.

saved my day, thank u very much... but why it happen tho?

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

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.

You saved my life! thank you!

Thank you so much, everyone. Empty folders were indeed the case.

I did some googling and found out that Heroku doesn't exclude empty folders, Git does.
If you're using GitHub along with Heroku, you can clearly see that there are no empty folders. In Heroku CLI, if you do a heroku run dir , you can see the that the empty folders are missing. The usual workaround for this is to keep a file called .gitkeep in the empty folders.

Source: https://stackoverflow.com/a/44020216/12172493

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
}

This saves my day. Thank you @LinusU

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.

Thank you a million times!! It worked!!!!!!!!!!!!!!!!!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

arsinawaz picture arsinawaz  Â·  4Comments

BlueOctober picture BlueOctober  Â·  3Comments

NobleUplift picture NobleUplift  Â·  4Comments

Gabxi picture Gabxi  Â·  3Comments

ChristianRich picture ChristianRich  Â·  4Comments