Asynchronous fs methods in Node.js do not block the event loop and handle multiple operations concurrently, improving performance while Synchronous fs methods block the event loop until the operation completes, which can lead to inefficiencies and slower performance for I/O-bound tasks.
Table of Content
FS Module in Node
The fs (File System) module in Node.js provides various methods for interacting with the file system of your operating system. These methods can be categorized into two main types: synchronous and asynchronous. Understanding the difference between these two types is crucial for writing efficient and responsive Node.js applications.
Syntax:
npm install fs --saveNote: The npm in the above command stands for node package manager from where all the dependencies can be installed in NodeJS.
For using the fs module, append the following statement in the code:
const fs = require('fs');The fs module offers various file-handling operations, including reading files, writing files, appending files, closing files, and deleting files. These operations can be performed synchronously or asynchronously, depending on the user's requirements.
Synchronous Methods
Synchronous functions block the execution of the program until the file operation is performed. These functions are also called blocking functions. The synchronous methods have File Descriptor as the last argument. File Descriptor is a reference to opened files. It is a number or a reference id to the file returned after opening the file using fs.open() method of the fs module. All asynchronous methods can perform synchronously just by appending "Sync" to the function name. Some of the synchronous methods of fs module in NodeJS are:
- fs.readFileSync()
- fs.renameSync()
- fs.writeSync()
- fs.writeFileSync()
- fs.fsyncSync()
- fs.appendFileSync()
- fs.statSync()
- fs.readdirSync()
- fs.existsSync()
Example 1: Synchronous read method
Step 1: Let's create a JavaScript file named main.js and a text file with the name sample.txt having the following statement.
GeeksForGeeks is a Computer Science portal.Step 2: Add the following code inside main.js file and execute it:
var fs = require("fs");
// Synchronous read
console.log("Synchronous read method:");
var data = fs.readFileSync('sample.txt');
console.log("Data in the file is - " + data.toString());
Output:

Example 2: Synchronous append method
Step 1: Let's create a JavaScript file named main.js and a text file with the name sample.txt having the following statement.
Hello World !Step 2: Add the following code inside main.js file and execute it.
var fs = require("fs");
// Synchronous read
console.log("Synchronous append method:");
var data = "\nGeeksForGeeks is a Computer Science portal.";
// Append data to file
fs.appendFileSync('sample.txt', data, 'utf8');
console.log("Data is appended to file successfully.")
data = fs.readFileSync('sample.txt');
console.log("Data in the file after appending is - \n" + data.toString());
Output:

Asynchronous Methods
Asynchronous functions do not block the execution of the program and each command is executed after the previous command even if the previous command has not computed the result. The previous command runs in the background and loads the result once it has finished processing. Thus, these functions are called non-blocking functions. They take a callback function as the last parameter. Asynchronous functions are generally preferred over synchronous functions as they do not block the execution of the program whereas synchronous functions block the execution of the program until it has finished processing. Some of the asynchronous methods of fs module in NodeJS are:
- fs.readFile()
- fs.rename()
- fs.write()
- fs.writeFile()
- fs.fsync()
- fs.appendFile()
- fs.stat()
- fs.readdir()
- fs.exists()
Heavy operations which consume time for processing such as querying huge data from a database should be done asynchronously as other operations can still be executed and thus, reducing the time of execution of the program.
Example 1: Asynchronous read method
Step 1: Let's create a JavaScript file named main.js and a text file with the name sample.txt having the following statement:
GeeksForGeeks is a Computer Science portal.Step 2: Add the following code inside main.js file and execute it.
var fs = require("fs");
// Asynchronous read
console.log("Asynchronous read method:");
fs.readFile('sample.txt', function (err, data) {
if (err) {
return console.error(err);
}
console.log("Data in the file is - " + data.toString());
});
Output:

Example 2: Asynchronous append method
Step 1: Let's create a JavaScript file named main.js and a text file with the name sample.txt having the following statement:
Hello World !Step 2: Add the following code inside main.js file and execute it:
var fs = require("fs");
const data = "\nGeeksForGeeks is a Computer Science portal.";
// Asynchronously appending data to file
fs.appendFile('sample.txt', data, 'utf8',
// Callback function
function(err) {
if (err) throw err;
// If no error
console.log("Data is appended to file successfully.")
});
fs.readFile('sample.txt', function (err, data) {
if (err) {
return console.error(err);
}
console.log("Data in the file after appending: \n"
+ data.toString());
});
Output:

Difference between Asynchronous and Synchronous methods
Synchronous methods | Asynchronous methods |
| Synchronous functions are called blocking functions | Asynchronous functions are called non-blocking functions. |
| It blocks the execution of the program until the file operation has finished processing. | It does not block the execution of the program. |
| These functions take File Descriptor as the last argument. | These functions take a callback function as the last argument. |
| Examples: fs.readFileSync(), fs.appendFileSync(), fs.writeFileSync() etc. | Examples: fs.readFile(), fs.appendFile(), fs.writeFile(), fs.stat() etc. |
When to Use Each
- Synchronous Methods: Use synchronous methods when simplicity and sequential execution are more important than performance, such as in scripts or small applications where file operations are minimal.
- Asynchronous Methods: Use asynchronous methods when performance and responsiveness are crucial, especially in large-scale applications or web servers where multiple operations need to be performed concurrently without blocking the event loop.