Hey, I’ve created this Express rest api that let’s you save recipes including an image using Multer, I save the file uploaded by the user temporarily in the server and after the recipe is created I delete it.
It works locally, as always, but in production I’m getting this error:
ENOENT: no such file or directory, open ‘/opt/render/project/src/Images/1691631583743.png’
This is my multer setup, which I feel could be the source of the issue.
const storage = multer.diskStorage({
destination: (req, file, cb) => cb(null, path.join(process.cwd(), “/Images”)),
filename: (req, file, cb) => {
const ext =
path.extname(file.originalname) || .${file.mimetype.split("/")[1]}
;
return cb(null, Date.now() + ext);
},
});
And here is the controller that tries to read the file
req.body.image = fs.readFileSync(
path.join(process.cwd(), "/Images/", req.file.filename)
);
It seems that it cannot find such directory. I’ve tried using __dirname, process.cwd() and setting the Images folder to serve static files using express.static to set the correct path with no success.
I understand that since I’m using the free tier files will be lost on redeploys and such, but that shouldn’t be an issue since I just temporarily save them.
Build process does not show the problem either.
I would really appreciate any help, the repo is the following: