2

I am stacked with this error ENOENT: no such file or directory, open 'public/images/name.ext from express-fileupload I know there are lot of the same issue, but for me seems the solution do not work the ones I found here: No such file or directory error when uploading a file using Express-fileupload - Node.js

neither this one: ENOENT: no such file or directory .?

And I am still getting this error. I am not sure weather it is directory public declaration or mv('directory specification') or file name. Something got to be wrong definitely

Here is my project folder structure: enter image description here

and here is my app.js file

const express = require('express');
const cors = require('cors');
const fileUpload = require('express-fileupload')
const passport = require("passport");
const path = require("path");

const {db} = require("./database/db");
const authMiddleware = require('./app/middleware/authMiddleware')

const app = express();
// app.use(express.static(path.join(__dirname, "./public/")));
app.use(express.static(__dirname));
// app.use(express.static('public'))
// app.use('/public', express.static('public'));

// console.log('static path', express.static('public'))
app.use(fileUpload())


app.use('/api/asset/:assetId/uploadimage', async(req, res, next) => {
    const file = req.files.file;
    console.log('files bs',file)
    try{
        if(file === null){
            throw new Error('Not file Specified')
        }
        // await file.mv(path.join(__dirname, `public/images/${file.name}`))
        // await file.mv('public/image/' + file.name)

        await file.mv('public/images/' + file.name)

        res.status(200).json({fileName: file.name, filePath: `/uploads/${file.name}`});
    }
    catch(err){
        console.log(err)
        return res.status(400).send(err.message);
    }
})

You can see the commented code, these are the ways I also tried and they did not work :(

and here is the error:

node_1   | files bs {
node_1   |   name: '20191009_122021.jpg',
node_1   |   data: <Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 01 00 00 48 00 48 00 00 ff e1 02 8a 45 78 69 66 00 00 4d 4d 00 2a 00 00 00 08 00 09 01 0f 00 02 00 00 00 08 00 00 ... 1488306 more bytes>,
node_1   |   size: 1488356,
node_1   |   encoding: '7bit',
node_1   |   tempFilePath: '',
node_1   |   truncated: false,
node_1   |   mimetype: 'image/jpeg',
node_1   |   md5: 'd2855d664bd5e5565a45da1ae06bd1ee',
node_1   |   mv: [Function: mv]
node_1   | }
node_1   | [Error: ENOENT: no such file or directory, open 'public/images/20191009_122021.jpg'] {
node_1   |   errno: -2,
node_1   |   code: 'ENOENT',
node_1   |   syscall: 'open',
node_1   |   path: 'public/images/20191009_122021.jpg'
node_1   | }

Please help :)

3 Answers3

1

UPDATE FOR THOSE WHO HAVE THE UPLOAD FUNCTION NOT IN THE INDEX.JS FILE FOR EXAMPLE IN /APP_NAME/ROUTES OR /APP_NAME/SERVER/SRC/ROUTE/ROUTE.JS (mern_stack version)

So in my case I have made upload function in the /app/controllers/ folder

My controller file location structure:

/my_app_name/server/src/app/controllers/FileUploader.js

My root server/index file location:

/my_app_name/server/src/app.js

in order to specify location for public/images where i want to store my images:

/my_app_name/server/src/public/images

in the /my_app_name/server/src/app/controllers/FileUploader.js I had to use require('path')

and this is how it looks like:

const path = require("path");

const addImages = async(req, res, next) => {
    const file = req.files.file;
    console.log('files',file)
    try{
        if(file === null){
            throw new Error('Not file Specified')
        }
        await file.mv(path.join(path.dirname('/app/server/src/'), '/src/public/images/') + file.name)

        res.status(200).json({fileName: file.name, filePath: `/public/images/${file.name}`});
    }
    catch(err){
        console.log(err)
        return res.status(400).send(err.message);
    }
}

explanation for the reason why i had to use path.join() and path.dirname()

basically I could not use (path.join(__dirname, "./public/images")) or __dirname in general since it would give me out the path where is my FileUploader.js is, which is /my_app_name/server/src/app/controllers/ and then if i would of added "./public/images" it would of looked into /my_app_name/server/src/app/controllers/public/images Therefore we need to specify the path.dirname() where is your root node folder is in my case it would be path.dirname('/app/server/src/') since i have such sloppy MERN stack structure with docker

and the rest you just need to join your dirname with your public/images location which is path.join(path.dirname('/app/server/src/'), '/src/public/images/')

so All together it must be await file.mv(path.join(path.dirname('/app/server/src/'), '/src/public/images/') + file.name) (IF YOU HAVE THE SAME SLOPPY MERN STACK STRUCTURE LIKE ME :) )

I hope it helps somebody! Happy geeking ! :)

0

Update from myself. I manage to resolve the issue.

Seems like the node doesn't like this syntax of specifying the path with this `` grave accent symbol therefore it was giving me error. SO for those who still struggling try to specify the path like this.

await file.mv(path.join(__dirname, 'public/images/') + file.name)

UPDATE: make sure to have the specified path in your public folder. to clarify, you need to create first directory your_project_name/public/images/

in my case it was

mern
 -server
   -src
     -public
       -images

     app.js

  serve.package.json
 -client
  -src
0

when I was trying to do this I also got the same error but thing is when we give the path for ex, we write __dirname, I had created a upload section in my root directory but the __dirname was accessing the inner files like it was not accessing the rootfolder/upload but it was accessing rootfolder/server/controllers/upload and this was the path where I was working, it means __dirname is accessing the wrong path that's why this error is occurring.

Solution is

exports.serviceDataUpload = (req, res) => {
let sampleFile; // 
let uploadpath;
if (!req.files || Object.keys(req.files).length === 0) {
    return res.status(400).send('No file were uploaded')
} // If user has not uploaded any thing
sampleFile = req.files.uploadImg; //Getting file from body
uploadpath = 'C:/Users/Rahul/Desktop/Coding Projects/Prompt/upload/' + sampleFile.name; //Here is the main error accurring, write it the way i have written
sampleFile.mv(uploadpath, function (err) {
    if (err) return res.status(500).send(err);
    res.send('File Uploaded!')
});

}

You can see how i have written the uploadPath, don't go with __dirname just simply write the complete path and concat it with the the object's key. So in my case its sampleFile.name