Open In App

Node JS fs.readFileSync() Method

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

The fs.readFileSync() method is an inbuilt application programming interface of the fs module which is used to read the file and return its content. In the fs.readFile() method, we can read a file in a non-blocking asynchronous way, but in the fs.readFileSync() method, we can synchronously read files, i.e. we are telling node.js to block other parallel processes and do the current file reading process. That is, when the fs.readFileSync() method is called the original node program stops executing

Syntax:  

fs.readFileSync( path, options )

Parameters:  

  • path: It takes the relative path of the text file. The path can be of URL type. The file can also be a file descriptor. If both the files are in the same folder just give the filename in quotes.
  • options: It is an optional parameter that contains the encoding and flag, the encoding contains data specification. Its default value is null which returns the raw buffer and the flag contains an indication of operations in the file. Its default value is ‘r’.

Return Value: This method returns the content of the file.

Example 1:  “This is some text data ” which is stored in input.txt file.

javascript




// Node.js program to demonstrate the
// fs.readFileSync() method
 
// Include fs module
const fs = require('fs');
 
// Calling the readFileSync() method
// to read 'input.txt' file
const data = fs.readFileSync('./input.txt',
    { encoding: 'utf8', flag: 'r' });
 
// Display the file data
console.log(data);


Output: 

This is some text data which is stored in input.txt file.

Now, the question is how is this fs.readFileSync() method is different from fs.readFile() method. An example where we can find out when to use fs.readFileSync() and fs.readFile() methods.
Let’s say there are two input files input1.txt and input2.txt and both files are saved in the same folder.

Example 2:  

  • “(1) This is some text data” which is stored in input1.txt file.
  • “(2) This is some text data” which is stored in input2.txt file.

javascript




// Node.js program to demonstrate the
// fs.readFileSync() method
 
// Include fs module
const fs = require('fs');
 
// Calling the fs.readFile() method
// for reading file 'input1.txt'
fs.readFile('./input1.txt',
    { encoding: 'utf8', flag: 'r' },
    function (err, data) {
        if (err)
            console.log(err);
        else
            console.log(data);
    });
 
// Calling the fs.readFileSync() method
// for reading file 'input2.txt'
const data = fs.readFileSync('./input2.txt',
    { encoding: 'utf8', flag: 'r' });
 
// Display data
console.log(data);


Output: 

(1) This is some text data which is stored in input1.txt file.
(2) This is some text data which is stored in input2.txt file.

Observation: The order of file reading in Node.js is influenced by the asynchronous nature of `fs.readFile()` and the synchronous nature of `fs.readFileSync()`. The event loop determines the execution order, and using `fs.readFileSync()` can block parallel processes, impacting the observed file read order.



Last Updated : 25 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads