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

Published April 1, 2021

To check if a file exists asynchronously, you can use the access() method in the fs (filesystem) module in Node.js.

Let's say I want to check whether the path /myFolder/myFile.txt to my file exists or not. Here we can use the fs.access() method and pass 2 arguments:

  • the first one is the path to the file to check
  • the second one is an error first callback function. The callback function will be passed an error object if any error has occurred.

It can be done like this,

// require filesystem module
const fs = require("fs");

// check if file exist
// using fs.access() method
fs.access("./myFolder/myFile.txt", (error) => {
  //  if any error
  if (error) {

  console.log("File Exists!");

See the above coed live in

That's it! 😃

Feel free to share if you found this useful 😃.

Share on: Facebook Twitter