noddy asked this 4 years ago

Node.js check if file exists asynchronously

How to check if a file exists in Node.js? How to check this asynchronously?


Best Answer by mkbaines 4 years ago

Use fs.access(path, mode, callback) or fs.stat(path,callback) or fs.lstat(path, callback)

However it is not recommended to do such a check before read/write or any operation on the file because it may cause a race condition. In such a case do the error handling within fs.open() or fs.readFile() or fs.writeFile()