I'm reading the manual for stat method here and it says:
Using fs.stat() to check for the existence of a file before calling fs.open(), fs.readFile() or fs.writeFile() is not recommended. Instead, user code should open/read/write the file directly and handle the error raised if the file is not available.
To check if a file exists without manipulating it afterwards, fs.access() is recommended.
So, I've got two questions:
- Why using error handler is preferred way over - fs.stat()to check for file existence?
- And since I can use - fs.access()to check for file existence, is using- error handlermechanism still preferred way to ensure file is open?
I think I have found an answer to the second question:
Using fs.access() to check for the accessibility of a file before calling fs.open(), 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.
So probably fs.open() blocks file for other processes, while fs.stat() and fs.access() simply request information and other processes still can change/delete the file.
 
     
    