title: How to not overwrite file in node.js
body: <p>I want to make this code to change filename if file exists instead of overwritng it.</p>
  
  <pre><code>var fileName = 'file';
  
  fs.writeFile(fileName + '.txt', 'Random text', function (err) {
    if (err) throw err;
    console.log('It\'s saved!');
  });
  </code></pre>
  
  <p>Something like:</p>
  
  <pre><code>var fileName = 'file',
      checkFileName = fileName,
      i = 0;
  
  while(fileExists(checkFileName + '.txt')) {
    i++;
    checkFileName = fileName + '-' + i;
  } // file-1, file-2, file-3...
  
  fileName = checkFileName;
  
  fs.writeFile(fileName + '.txt', 'Random text', function (err) {
    if (err) throw err;
    console.log('It\'s saved!');
  });
  </code></pre>
  
  <p>How can I make "fileExists" function, considering that <code>fs.exists()</code> is now deprecated and <code>fs.statSync()</code> or <code>fs.accessSync()</code> throws error if file doesn't exist. Maybe there is a better way to achieve this?</p>