1

I've been searching for a simple way to create a function that checks for a filename within a folder to be used in an if/else scenario.

Example:

if(fs.exists('./file.txt')){ //Do Something };
else { //Do Something Else };

Nothing seems to work for me, although I'm sure it must be fairly simple and straight-forward. All of my searches are littered with deprecated methods that don't really seem to be helpful. Anything is helpful at this point. Thanks in advance!

Patrick Hund
  • 19,163
  • 11
  • 66
  • 95
  • Hi Austin, take a look at the duplicate link above: `fs.exists` is an asynchronous function that accepts a callback as argument. What you're looking for is `fs.existsSync` – Patrick Hund Dec 04 '18 at 10:29
  • Just a note, if you are working with the fs, you'll probably want to work with async methods for anything in production. If it is syncronous you'll be blocking the eventloop in the case that you have to wait on the fs. – unflores Dec 04 '18 at 10:32

0 Answers0