I am currently trying to figure out the "correct" way to check if a local folder exists, before saving a file into it and am a little bit confused by the nodejs docs.
fs.exists() is deprecated and you should use fs.stat() or fs.access(). So far so good.
fs.stat():
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.
This states, that I should just try to write, catch the error, create the folder, and try again. Also fine by me, even though I am moving/renaming the file , so I don't directly use one of the three mentioned functions.
then, the doc says:
To check if a file exists without manipulating it afterwards, fs.access() is recommended.
Since I am not really modifying the file, but "only" it's content, one might argue that this is the way to go.
But then again, the fs.access() documentation goes into detail why that's a bad idea, too:
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.
Yadda yadda yadda, there are already somewhat related questions (here, or here), but is there any official info on the "best practice" that's not more than two years old?