A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/node-js/node-js-fs-readfilesync-method/ below:

Node JS fs.readFileSync() Method - GeeksforGeeks

Node JS fs.readFileSync() Method

Last Updated : 12 Jul, 2025

In NodeJS, the fs.readFileSync() method is used to read files from the filesystem in a synchronous manner. This means that when you use this method, your program will pause and wait until the file has been completely read before moving on to the next task.

This behavior can be useful when you need to ensure that the file's content is available immediately, such as during the initial setup of your application.​

Syntax:

fs.readFileSync(path, options);

In this syntax:

Return Value:

How does fs.readFileSync() work?

When you call fs.readFileSync(), NodeJS reads the file from the disk and blocks the rest of the program from executing until the file has been completely read. This ensures that the file's data is available immediately after the method call, which can be beneficial in certain situations but may affect performance if used improperly.​

Now, let us understand with the help of the example:

Suppose you have a file named input.txt with the following content:

This is some text data stored in input.txt.

You can read this file synchronously using fs.readFileSync() as follows:

javascript
// Include the fs module
const fs = require('fs');

// Read the file synchronously
const data = fs.readFileSync('./input.txt', { encoding: 'utf8', flag: 'r' });

// Display the file content
console.log(data);

Output:

This is some text data stored in input.txt.
fs.readFileSync() vs. fs.readFile()

Understanding the difference between fs.readFileSync() and fs.readFile() is important for effective file handling in NodeJS:​

Now, let us understand with the help of the example:

javascript
// Include the fs module
const fs = require('fs');

// Asynchronously read 'input1.txt'
fs.readFile('./input1.txt', { encoding: 'utf8', flag: 'r' }, (err, data1) => {
  if (err) {
    console.error('Error reading input1.txt:', err);
  } else {
    console.log('input1.txt content:', data1);
  }
});

// Synchronously read 'input2.txt'
try {
  const data2 = fs.readFileSync('./input2.txt', { encoding: 'utf8', flag: 'r' });
  console.log('input2.txt content:', data2);
} catch (err) {
  console.error('Error reading input2.txt:', err);
}

Output:

input1.txt content: (Content of input1.txt)
input2.txt content: (Content of input2.txt)
Observation: When to Use fs.readFileSync() and fs.readFile() Use fs.readFileSync() : Use fs.readFile() : When to Avoid fs.readFileSync() 1. Performance Concerns: 2. Non-Blocking Operations in Web Servers: Conclusion

The fs.readFileSync() method in NodeJS provides a straightforward way to read files synchronously, ensuring that the file's content is available immediately after the method call. However, it's important to use this method judiciously, as it blocks the event loop and can impact performance, especially in applications that require high concurrency. For non-blocking file reading, consider using the asynchronous fs.readFile() method.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4