Error: "ENOENT: no such file or directory" while uploading files to server

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:

Hi,

The multer docs note:

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.

You code snippet shows you’re using a function for the destination.

There doesn’t seem to be an Images folder in your repo, maybe you need to create the Images folder first? (also be aware Linux has a case-sensitive filesystem).

Alan

Hey, thanks alot sir. I did not realize that the Images folder was not in the repo. The folder did exist locally, but I made the mistake of pushing it empty, so git was not tracking it.

Thanks again for the help.

Hello, I’m facing a similar issue.

I’m using multer to upload XML file temporarily in order to convert it to a pdf file. Here is my multer setup:

let storage = multer.diskStorage({
  destination: (req, file, cb) => {
    cb(null, path.join(__dirname, 'uploads'));
  },
  filename: (req, file, cb) => {
    cb(null, file.originalname);
  },
});
let upload = multer({ storage });
const xmlFilePath = path.join(__dirname, '..', 'uploads', AddFun.changeExtensionFileName(nameFile, 'xml'));
                const data = fs.readFileSync(xmlFilePath, { encoding: 'utf8', flag: 'r' });
                const json = parseXmlToJson(data);
                content = style + createHtmlWithJson(json);

Locally it works, the files are stored in the ‘uploads’ folder, also in the github repo there is an ‘uploads’ folder with data inside so in this case i cant understand why I’m getting:

Error: ENOENT: no such file or directory, open '/opt/render/project/src/uploads/BOLETAEB01-110763504030.xml

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.