Solved: node check if file exists

The main problem with checking if a file exists is that it can be slow.


var fs = require('fs');

if (fs.existsSync('file.txt')) {
  // do something
}

This code is checking if a file called ‘file.txt’ exists in the current directory. If it does, then the code inside the if statement will run.

working with files

There are a few ways to work with files in JavaScript. The most common way is to use the File API. You can also use the FileReader and FileWriter objects to read and write files.

if file exists

if (file.exists()) { // do something with file }

Related posts:

Leave a Comment