Open In App

Node fs.existsSync() Method

Last Updated : 10 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Node.js, the fs.existsSync() method checks if a file or folder exists at a given path. It’s synchronous, meaning it pauses the program until it finds the result (either true if it exists, or false if it doesn’t). Because it stops everything while it works, it’s best used for quick checks in small programs.

Syntax:

fs.existsSync( path)

Parameters:

  • path: The path of the file or directory you want to check. It can be a string, buffer, or URL.

Return Value: boolean:

  • Returns true if the file or directory exists.
  • Returns false if the file or directory does not exist.

How does it work?

It checks whether a file or folder exists at the specified path.

  • Path: You give it the path to a file or folder you want to check.
  • Check: It looks on your system to see if that file or folder is there.
  • Result: It gives back true if the file or folder exists, or false if it doesn’t.

When to use fs.existsSync()

Use it for quick checks and simple validation tasks.

  • Quick Checks: Use it when you just need to quickly check if a file or folder exists before doing something else.
  • Simple Validation: It’s great for simple tasks like checking if a file is there before opening or reading it.
  • Non-Critical Operations: Use it when it’s okay if the program pauses.

Example 1: Below programs illustrate the fs.existsSync() method in Node.js.

JavaScript
// Node.js program to demonstrate the 
// fs.existsSync() method 

// Import the filesystem module 
const fs = require('fs');

// Get the current filenames 
// in the directory 
getCurrentFilenames();

let fileExists = fs.existsSync('hello.txt');
console.log("hello.txt exists:", fileExists);

fileExists = fs.existsSync('world.txt');
console.log("world.txt exists:", fileExists);

// Function to get current filenames 
// in directory 
function getCurrentFilenames() {
	console.log("\nCurrent filenames:");
	fs.readdirSync(__dirname).forEach(file => {
		console.log(file);
	});
	console.log("\n");
}

Output:

Current filenames:
hello.txt
index.js
package.json

hello.txt exists: true
world.txt exists: false

Example 2: Below programs illustrate the fs.existsSync() method in Node.js.

JavaScript
// Node.js program to demonstrate the
// fs.existsSync() method
// Import the filesystem module

const fs = require("fs");

// Get the current filenames
// in the directory
getCurrentFilenames();

// Check if the file exists
let fileExists = fs.existsSync("hello.txt");
console.log("hello.txt exists:", fileExists);

// If the file does not exist
// create it
if (!fileExists) {
	console.log("Creating the file");
	fs.writeFileSync("hello.txt", "Hello World");
}

// Get the current filenames
// in the directory
getCurrentFilenames();

// Check if the file exists again
fileExists = fs.existsSync("hello.txt");
console.log("hello.txt exists:", fileExists);

// Function to get current filenames
// in directory
function getCurrentFilenames() {
	console.log("\nCurrent filenames:");
	fs.readdirSync(__dirname).forEach((file) => {
		console.log(file);
	});
	console.log("\n");
}

Output:

Current filenames:
hello.txt
index.js
package.json
hello.txt exists: true
Current filenames:
hello.txt
index.js
package.json
hello.txt exists: true

Summary

the existsSync method of Node file system module verifies if the mentioned file exists in the current directory. If it is present it returns true else it return false.


Explore this File System Module Complete Reference to uncover detailed explanations, advanced usage examples, and expert tips for mastering its powerful features. Enhance your Node.js applications with comprehensive insights into file operations, error handling, and efficient directory management.



Next Article

Similar Reads