Tobiq Tobiq - 8 months ago 59
Node.js Question

Node JS - No such file?

I'm using this piece of code to delete a file on demand

fs.access(path, (err)=> err || fs.unlink(path));

I got this error

Error: ENOENT: no such file or directory, unlink 'C:\ ... '
at Error (native)

Which makes no sense to me as I literally just checked for the files existence before attempting the unlink - I have a feeling something weird's going on behind the scenes like file locking.

How do I rectify this error?

Also, do I need to lock the file myself before I attempt to delete, to guarantee a robust and safe delete. I won't be there to manually delete the file and restart the server every time a user tries to delete their file.

amj amj

calling fs.access before write or delete is not recommended. Please check the below link

Using fs.access() to check for the accessibility of a file before calling, fs.readFile() or fs.writeFile() is not recommended. Doing so introduces a race condition, since other processes may change the file's state between the two calls. Instead, user code should open/read/write the file directly and handle the error raised if the file is not accessible.