title: NodeJs: fs.stat() or fs.access() to check if a folder exists?
body: <p>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.</p>
  
  <p><a href="https://nodejs.org/api/fs.html#fs_fs_exists_path_callback" rel="noreferrer">fs.exists()</a> is deprecated and you should use fs.stat() or fs.access(). So far so good.</p>
  
  <p>fs.stat():</p>
  
  <blockquote>
    <p>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.</p>
  </blockquote>
  
  <p>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.</p>
  
  <p>then, the doc says:</p>
  
  <blockquote>
    <p>To check if a file exists without manipulating it afterwards, fs.access() is recommended.</p>
  </blockquote>
  
  <p>Since I am not really modifying the file, but "only" it's content, one might argue that this is the way to go.</p>
  
  <p>But then again, the fs.access() documentation goes into detail why that's a bad idea, too:</p>
  
  <blockquote>
    <p>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.</p>
  </blockquote>
  
  <p>Yadda yadda yadda, there are already somewhat related questions (<a href="https://stackoverflow.com/questions/41819917/how-to-check-if-directory-exists-before-writing-to-it-nodejs">here</a>, or <a href="https://stackoverflow.com/questions/40259928/why-is-using-fstat-not-recommended">here</a>), but is there any official info on the "best practice" that's not more than two years old?</p>