Check If a file or folder exist in NodeJS | Javascript

Check If a file or folder exist in NodeJS | Javascript

In NodeJS, we can interact with the file system to check if a file exist in our hard disk at a specified path.

NodeJS comes with fs core module which let us interact with the file system of our computer.

In this post we will learn about 3 methods inside fs module to check if a certain file or folder exist in the file-system:

  • fs.existsSync()
  • fs.accessSync()
  • fs.access()

Let's learn about the methods with some examples below.

Checking if a file exist using fs.existsSync() method

The fs.existsSync() is a synchronous method which checks if the file or folder exist by tracking the specified path from the directory where the code is executed. It will return true if the file exist or else it will return false if not found.



Since it is a synchronous method, it will stop the execution of the JavaScript code until it's process is completed.

Let's see fs.existsSync() in action:

const fs = require("fs");
const path = "./file.html";

if (fs.existsSync(path)) {
  console.log("file exist");
} else {
  console.log("file do not exist");

Checking if a file exist using fs.accessSync()

The fs.accessSync() method is also a synchronous method that checks the existence or the access permission of the file or folder.


fs.accessSync(path, mode)

The fs.accessSync() accepts two parameters:

path: It defines the path of the file or the folder.

mode: It is an optional parameter. It define how the check process will be executed. It have four constants (Node File Access Constants):

fs.constants.F_OKchecks for existence of file
fs.constants.R_OKchecks for read permission
fs.constants.W_OKchecks for write permission
fs.constants.X_OKchecks for execute permission

The default value of the mode parameter is fs.contants.F_OK . And the logical OR (|) can be use to give multiple permissions.

In the example below, we will use try...catch block to check for the file with read and write mode(optional). If not found, it will throw us an error.

const fs = require("fs");
const path = "./myFile.txt";
// for folder const path = "./foldername"
try {
  fs.accessSync(path, fs.constants.R_OK | fs.constants.W_OK);
  console.log("File exist");
} catch (err) {
  console.log("File do not exist");

Using fs.access() to check if a file exist (asynchronous)

The fs.access() method is an asynchronous method to test the permission and existence of a specified file or folder in the file-system.


fs.access( path, mode, callback )

This method takes in three parameters:

path : location of the file or folder in your computer.

mode : It is optional. It check how the process will be executed. Contains 4 constant same as fs.accessSync() method mentioned above.

callback : the function that will be executed when the method is called. We can pass an error which will be executed if the method fails.

Let us see an example:

const fs = require("fs");
const path = "./myFile.txt";
// for folder const path = "./foldername"

fs.access(path, fs.constants.R_OK | fs.constants.W_OK, function (error) {
  if (error) {
    console.log("File Not Found");
  } else {
    console.log("File found");

Related Topics:

How to read text file in JavaScript (line by line)