How to Check If File Exists in Node.js (Synchronously and Asynchronously)

Here are five ways to check if a file exists in Node.js.

  1. Using fs.existsSync() (Synchronous)
  2. Using fs.promises.access() (Asynchronous with Promises)
  3. Using fs.access() with Callback (Asynchronous with Callback)
  4. Using “stat” methods
  5. Using “file” methods

Method 1: Using fs.existsSync() (Synchronous)

To check if a file exists synchronously in Node.js, use the “fs.existsSync()” method. The fs.existsSync() method returns a boolean suggesting whether the file exists.

For this example, we will check the app.txt file, my current project folder.

/Users/krunallathiya/Desktop/Code/pythonenv/env/data.txt

Create a server.js file in your project directory and add the below code.

const fs = require('fs');

if (fs.existsSync('data.txt')) {
  console.log('File exists');
} else {
  console.log('File does not exist');
}

Output

File exists

Method 2: Using fs.promises.access() (Asynchronous with Promises)

The fs.promises.access() method is used to access a file asynchronously and throws an error if the file does not exist. It’s often used with async/await for cleaner code.

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

const fs = require('fs').promises

async function checkFileExists(filePath) {
  try {
    await fs.access(filePath);
    console.log('File exists');
  } catch (error) {
    console.log('File does not exist');
  }
}

checkFileExists('data.txt');

Output

File exists

Method 3: Using fs.access() with Callback (Asynchronous with Callback)

This is the callback-based version of the previous approach.

const fs = require('fs')

fs.access('data.txt', fs.constants.F_OK, (err) => {
  if (err) {
    console.log('File does not exist');
  } else {
    console.log('File exists');
  }
});

Output

File exists

Method 4: Using “stat” methods

The fs.stat() and fs.statSync() methods in Node.js are used to get the information about a file or directory. If the file or directory does not exist, these methods will throw an error. Therefore, you can use them to check the existence of a file or directory.

Using fs.statSync() (Synchronous)

The fs.statSync() method returns a Stats object that contains information about the file. If the file does not exist, it throws an error.

const fs = require('fs')

try {
  let stats = fs.statSync('data.txt');
  if (stats.isFile()) {
    console.log('File exists');
  }
} catch (error) {
   console.log('File does not exist');
}

Output

File exists

Using fs.promises.stat() (Asynchronous with Promises)

This is the promise-based version.

const fs = require('fs').promises;

async function checkFileExists(filePath) {
  try {
    let stats = await fs.stat(filePath);
    if (stats.isFile()) {
      console.log('File exists');
    }
  } catch (error) {
    console.log('File does not exist');
  }
}

checkFileExists('data.txt');

Output

File exists

Method 5: Using “file” methods

Using file methods directly to check for the existence of a file is a bit more unconventional, but it can be done.

Using fs.openSync() (Synchronous)

The fs.openSync() method is used to open a file. If the file does not exist, it will throw an error.

const fs = require('fs');

try {
  let fd = fs.openSync('data.txt', 'r');
  fs.closeSync(fd);
  console.log('File exists');
} catch (error) {
  console.log('File does not exist');
}

Output

File exists

Using fs.promises.readFile() (Asynchronous with Promises)

The fs.promises.readFile() method is used to read a file. If the file does not exist, it will throw an error.

const fs = require('fs').promises;

async function checkFileExists(filePath) {
  try {
    await fs.readFile(filePath);
    console.log('File exists');
  } catch (error) {
    console.log('File does not exist');
  }
}

checkFileExists('data.txt');

Output

File exists

Conclusion

The fs.existsSync() method is “used to check synchronously if a file or directory exists in Node.js.”

The fs.promises.access()method is “used to check asynchronously if a file or directory exists in Node.js.”

Leave a Comment