Node.js | fs.existsSync() Method

The fs.existsSync() method is used to synchronously check if a file already exists in the given path or not. It returns a boolean value which indicates the presence of a file.

Syntax:

fs.existsSync( path )

Parameters: This method accepts a single parameter as mentioned above and described below:

  • path: It holds the path of the file that has to be checked. It can be a String, Buffer or URL.

Return Value: It returns a boolean value i.e true if the file exists otherwise returns false.

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



Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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");
}

chevron_right


Output:

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


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

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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");
}

chevron_right


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

Reference: https://nodejs.org/api/fs.html#fs_fs_existssync_path




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.