![]() |
VOOZH | about |
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.
fs.existsSync( path)Parameters:
Return Value: boolean:
It checks whether a file or folder exists at the specified path.
true if the file or folder exists, or false if it doesn’t.fs.existsSync()Use it for quick checks and simple validation tasks.
Example 1: Below programs illustrate the fs.existsSync() method in Node.js.
Output:
Current filenames:
hello.txt
index.js
package.json
hello.txt exists: true
world.txt exists: falseExample 2: Below programs illustrate the fs.existsSync() method in Node.js.
Output:
Current filenames:
hello.txt
index.js
package.json
hello.txt exists: true
Current filenames:
hello.txt
index.js
package.json
hello.txt exists: truethe 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.