I was going to use fs.access
to see if a directory exists, then if it exists then write a file into that directory, if it's not then first create the directory and then write a file into it.
But in the docs it says this is not recommended `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.`
Is there a recommended way to check first then write ?
fs.stat()
,fs.access()
or as other people have said, You can simply open the directory and make sureerr.code != ENOENT