Search code examples
node.jsfs

Error: ENOENT: no such file or directory, unlink


As you can see, there is a file at the path. But fs says no such file or directory. I can't understand why?

In another file, I can remove with the same code.

My boat.js file:

boat.findById(req.params.id,function(err, foundBoat) {
    if(err){
        console.log(err);
    }else{
        foundBoat.boatsFoto.forEach(function(path){
            console.log(typeof(path));
            fs.unlink("../public"+path,function(err){
                if(err) throw err;

                console.log('File deleted!');
            });
        });
    } 
});

And it is my error:

Error: ENOENT: no such file or directory, unlink '../public/uploads/akingokay/BoatsFoto/1524411110335kiralik-tekne.jpg'
at Error (native)

And you can see my file system


Solution

  • Can you try this instead:

    fs.unlink("public"+path,function(err){
        if(err) throw err;
    
        console.log('File deleted!');
    });