Node.js
File System
Directory Existence
Synchronous Operations
Coding Tips

Check synchronously if file/directory exists in Node.js

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

In Node.js, various filesystem (fs) methods are used to interact with files and directories. An important aspect of managing filesystem tasks is checking the existence of a file or directory. Ensuring a file or directory exists before performing operations on it is critical to avoid errors and ensure that your applications run smoothly.

Checking Existence Synchronously in Node.js

Node.js provides synchronous and asynchronous methods for interacting with the filesystem through the fs module. Synchronous methods will block the Node.js event loop while they execute, which can lead to performance issues in scalable applications. However, for certain scenarios, particularly scripts or simple backend applications where the impact on performance is minimal, synchronous methods can be preferable for their simplicity and ease-of-use.

The fs.existsSync(path) Method

The primary method for checking if a file or directory exists synchronously is fs.existsSync(). It takes a single argument—the path to the file or directory you want to check—and returns true if the file exists, and false otherwise.

Here's an example of how to use fs.existsSync():

javascript
1const fs = require('fs');
2
3const filePath = './example.txt';
4
5if (fs.existsSync(filePath)) {
6    console.log('The file exists.');
7} else {
8    console.log('The file does not exist.');
9}

This method is straightforward and works well for simple checks. However, it's important to note that fs.exists() was deprecated due to its inconsistent callback pattern and because it could not differentiate between true absence of the file and permission/access errors. Thus, the use of fs.existsSync() is generally acceptable in contexts where the impact of synchronous I/O is limited.

Alternatives: Using Try-Catch with fs.statSync() or fs.accessSync()

Using fs.existsSync() can sometimes be seen as an antipattern particularly when used in larger, more interactive applications because its synchronous nature can halt the execution of all other code. As an alternative, you can use fs.statSync() or fs.accessSync() within a try-catch block to check for the existence of a file or directory.

Example using fs.statSync():

javascript
1const fs = require('fs');
2
3const filePath = './example.txt';
4
5try {
6  const stats = fs.statSync(filePath);
7  console.log('File exists.');
8} catch (error) {
9  if (error.code === 'ENOENT') {
10    console.log('File does not exist.');
11  } else {
12    throw error;  // An error other than ENOENT occurred
13  }
14}

Example using fs.accessSync():

javascript
1const fs = require('fs');
2
3const filePath = './example.txt';
4
5try {
6  fs.accessSync(filePath, fs.constants.F_OK);
7  console.log('File exists.');
8} catch (error) {
9  console.log('File does not exist.');
10}

Both fs.statSync() and fs.accessSync() throw an error if the file does not exist, which can be caught and handled appropriately. fs.statSync() provides additional information about the file like size, creation date, etc., whereas fs.accessSync() is used to check user permissions.

When to Use Synchronous Methods

The use of synchronous methods in Node.js is appropriate under circumstances where the delay caused by blocking the event loop does not impact the application significantly. These circumstances might include:

  • Startup checks in an application (ensuring config files or essential data files are present)
  • Scripting in local and small-scale applications
  • When running operations in parallel is unnecessary

Summary Table

MethodUse caseReturnsBlocking
fs.existsSync()Quick existence checkBooleanYes
fs.statSync()Check existence with detailed file informationObjectYes
fs.accessSync()Check file accessibility (permissions)None (throws error if inaccessible)Yes

Conclusion

Checking whether a file or directory exists synchronously in Node.js can be efficiently performed using fs.existsSync(), fs.statSync(), or fs.accessSync(). Each method has its appropriate use cases and can be a valuable part of a Node.js developer's toolkit, especially in less complexity-constrained environments. However, for larger applications with high concurrency requirements, asynchronous methods are usually preferable to avoid blocking the Node.js event loop.


Course illustration
Course illustration

All Rights Reserved.