Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: ENOENT: no such file or directory,

When I'm trying to upload images and save in public/upload_files folder through postman it shows this error

node -v v10.15.3

npm -v 6.9.0

"Error: ENOENT: no such file or directory"

This is my code

const express = require('express'); 

const router = express.Router();    
const multer = require('multer');

const storage = multer.diskStorage({    
  destination: function(req, file, cb) {
    cb(null,'./public/uploaded_files');    
  },    
  filename: function(req, file, cb) {       
    cb(null,new Date().toISOString() + file.originalname);    
  } 
});

const upload = multer({storage:storage});    

router.post('/', upload.single('file'), (req,res,next) => {    
  console.log(req.file);
});

module.exports = router;

I'm just trying to save the images in the following folder public/upload_files

like image 298
cool Avatar asked Apr 20 '19 20:04

cool


People also ask

What does error Enoent mean?

It's an abbreviation of Error NO ENTry (or Error NO ENTity), and can actually be used for more than files/directories. It's abbreviated because C compilers at the dawn of time didn't support more than 8 characters in symbols. Follow this answer to receive notifications. edited Jun 4, 2019 at 14:30. community wiki.

What is Enoent in Nodejs?

The code ENOENT means that npm fails to open a file or directory that's required for executing the command. The npm start command is used to run the start script in the package. json file.25-Jun-2022.


1 Answers

I made few changes to my code and it worked.

I added this line

cb(null,path.join(__dirname,'../upload'))

and this

cb(null,Date.now() + path.extname(file.originalname))

code

var storage = multer.diskStorage({
    
destination: function(req, file, cb)
    
{
        
cb(null,path.join(__dirname,'../upload'))
 
},
    
filename: function(req, file, cb)
    
{
        
cb(null,Date.now() + path.extname(file.originalname))
    }
});
like image 175
cool Avatar answered Oct 03 '22 07:10

cool