JavaScript

How to Read Files with fs.readFile() in Node.js?

The “fs.readFile()” is the pre-defined method of the “fs(File System)” module. The “fs” module performs read, write, rename, update, and delete operations on the files by using its synchronous and asynchronous methods.

In these methods, the “fs.readFile()” is an asynchronous method that reads the entire content of the file. Its asynchronous nature does not block the execution of any other operations as in the “fs.readFileSync()” method. That’s why most developers prefer this method for reading a file because it never stops the whole program execution.

This post will provide a complete procedure to read a file with the “fs.readFile()” method.

How to Read Files with fs.readFile() in Node.js?

To read files with the help of the “fs.readFile()” method, follows its generalized syntax:

fs.readFile( filename, encoding, callback_function )

According to the above syntax the “fs.readFile()” works on three parameters that are listed below:

  • filename: It specifies the name of the file or its absolute path where it is placed.
  • encoding: It denotes the encoding type i.e. “utf-8” string format. It is “null” by default.
  • callback_function: It defines a callback function that executes after reading the specified file. It supports two parameters “err(If an error occurs)” and “data(the content of the sample file)”.

Now, use the above-stated syntax practically to read a file by following the given instructions.

Step 1: Sample File

As a sample file, a text file is created in the Node.js project having the dummy data as shown below:

Press “Ctrl+S” to save and “Ctrl+Z” to close the file.

Note: The entire content of the above sample file will be read using the Node.js “fs.readFile()” method.

Step 2: Apply “fs.readFile()” Method

Now, copy the following lines of code into the newly created “.js” file to read the sample file content named “file.txt”:

var fs = require('fs');

fs.readFile('file.txt', 'utf8', (err, data) => {

if (err) {

  console.error(err);

  return;

}

console.log(data);

});

In the above code snippet:

  • Firstly, the “fs” variable imports the File System module(fs) using the “require()” method.
  • Next, apply the “readFile()” method that specifies the given text file as its first argument, the encoding type “utf-8” as the second argument, and the “call-back” function as the third argument.
  • After that, the call-back function also specifies two default parameters “err” and “data”.
  • In this function definition, an “if” statement is specified that displays the error message(if occurs) by using the “console.error()” method. If an error does not occur, then the “console.log()” method is utilized that show the data of the specified file.

Output

Now, execute the following command on the terminal to run the “.js” file. It will display the specified file content:

node app.js

It can be seen that the terminal successfully displays the entire file content:

That’s all about reading a file with the “fs.readFile()” method.

Conclusion

To read files with the help of “fs.readFile()” method, use its generalized syntax that works on three parameters “path”, “encoding” and a “callback_function”. This method first stores the entire file content into a buffer and then returns it as the standard output. The “fs.readFile()” method is useful to read all types of files. This post has provided a complete procedure to read a file with the “fs.readFile()” module.

About the author

Areej Nadeem

I am a technical author holding a Bachelor’s degree in Computer Science. I am passionate about writing and learning new technologies and sharing my knowledge with the rest of the world.