Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: ENOENT: no such file or directory, unlink

Tags:

node.js

fs

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

like image 339
Rasit aydin Avatar asked Apr 22 '18 16:04

Rasit aydin


People also ask

What is Enoent?

The enoent meaning error no entry.

What is unlinkSync?

unlinkSync() method is used to synchronously remove a file or symbolic link from the filesystem. This function does not work on directories, therefore it is recommended to use fs. rmdir() to remove a directory.


2 Answers

Can you try this instead:

fs.unlink("public"+path,function(err){
    if(err) throw err;

    console.log('File deleted!');
});
like image 61
shubhambharti201 Avatar answered Oct 10 '22 09:10

shubhambharti201


You should first install the path module via CLI:

npm install path --save

and use it:

fs.unlink(path.join("public/" + path, photo.id + ".jpg"), function(response) {
  // handle the callback
});
like image 25
Raz Avatar answered Oct 10 '22 10:10

Raz