Getting Started With Node – Course

How to Check if file Exists

The simplest approach to check whether the file exists or not is by using the readFile() function. However, this function does open the file descriptor and takes some memory too.

If you just want to check the file existences in the system, I highly recommend the access() function.

Here is the code:

const fs = require('fs');

const path = './config.js';

fs.access(path, fs.F_OK, (err) => {
  if (err) {
  // file exists

This way we can check if a specified file exists in the given path.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.