Open In App

Node JS fs.readFile() Method

Last Updated : 25 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The fs.readFile() method is an inbuilt method that is used to read the file. This method read the entire file into the buffer. To load the fs module we use require() method. For example: var fs = require(‘fs’);

Syntax:  

fs.readFile( filename, encoding, callback_function )

Parameters: The method accepts three parameters as mentioned above and described below:  

  • filename: It holds the name of the file to read or the entire path if stored at another location.
  • encoding: It holds the encoding of the file. Its default value is ‘utf8’.
  • callback_function: It is a callback function that is called after reading of file. It takes two parameters:
    • err: If any error occurred.
    • data: Contents of the file.

Return Value: It returns the contents/data stored in file or error if any.

Steps to Create Node JS Application:

Step 1: In the first step, we will create the new folder by using the below command in the VScode terminal.

mkdir folder-name
cd folder-name

Step 2: Initialize the NPM using the below command. Using this the package.json file will be created.

npm init -y

Project Structure:

NodeProj

Project Structure

Example 1: Below examples illustrate the fs.readFile() method in Node JS. The output is undefined it means the file is null. It starts reading the file and simultaneously executes the code. The function will be called once the file has been read meanwhile the ‘readFile called’ statement is printed then the contents of the file are printed.

Javascript




// Node.js program to demonstrate
// the fs.readFile() method
 
// Include fs module
var fs = require('fs');
 
// Use fs.readFile() method to read the file
fs.readFile('Demo.txt', 'utf8', function(err, data){
    // Display the file content
    console.log(data);
});
 
console.log('readFile called');


Step to run the Node App:

node index.js

Output: 

readFile called
undefined

Example 2: Below examples illustrate the fs.readFile() method in Node JS:

javascript




// Node.js program to demonstrate
// the fs.readFile() method
 
// Include fs module
var fs = require('fs');
 
// Use fs.readFile() method to read the file
fs.readFile('demo.txt', (err, data) => {
    console.log(data);
 })


Step to run the Node App:

node index.js

Output:

undefined


Similar Reads

Node.js fsPromises.readFile() Method
The fsPromises.readFile() method is used to read the file. This method read the entire file into the buffer. To load the fs module, we use require() method. It Asynchronously reads the entire contents of a file. Syntax: fsPromises.readFile( path, options ) Parameters: The method accepts two parameters as mentioned above and described below: path: I
2 min read
Node.js filehandle.readFile() Method
The filehandle.readFile() method is used to asynchronously read the file contents. This method reads the entire file into the buffer. It Asynchronously reads the entire contents of a file. Syntax: filehandle.readFile( options ) Parameters: The method accepts single parameter as mentioned above and described below: options: It holds the encoding of
2 min read
How to operate callback-based fs.readFile() method with promises in Node.js ?
The fs.readFile() method is defined in the File System module of Node.js. The File System module is basically to interact with the hard disk of the user’s computer. The readFile() method is used to asynchronously read the entire contents of a file and returns the buffer form of the data. The fs.readFile() method is based on callback. Using callback
5 min read
Difference between readFile and createReadStream in Node.js
In this article, we will discuss the difference between readFile and createReadStream in Nodejs. Both are modules that allow us to open a file/stream and read data present in it. 1. readFile: fs module contains the readFile method. It is used to read a file by bringing it into the buffer. It is an asynchronous method and due to this, it reads the f
3 min read
PHP | readfile( ) Function
The readfile() function in PHP is an inbuilt function which is used to read a file and write it to the output buffer. The filename is sent as a parameter to the readfile() function and it returns the number of bytes read on success, or FALSE and an error on failure. By adding an '@' in front of the function name the error output can be hidden. Synt
2 min read
How to create a simple web server that can read a given file on a given disk via “readFile” function ?
To create a web server we need to import the 'http' module using require() method and create a server using the createServer() method of the HTTP module. Syntax: const http = require('http') const server = createServer(callback).listen(port,hostname); Now in the callback in the createServer() method, we need to read the file from our local director
3 min read
Node.js Connect Mysql with Node app
In this article, we will learn how to connect the Mysql database to the NodeJs application. Before we go deep into the coding part, let’s get a brief introduction about these technologies: NodeJs: An open-source platform for executing javascript code on the server side. Also a javascript runtime built on Chrome's V8 JavaScript engine. It can be dow
3 min read
Node.js Automatic restart Node.js server with nodemon
We generally type following command for starting NodeJs server: node server.js In this case, if we make any changes to the project then we will have to restart the server by killing it using CTRL+C and then typing the same command again. node server.js It is a very hectic task for the development process. Nodemon is a package for handling this rest
1 min read
How to resolve 'node' is not recognized as an internal or external command error after installing Node.js ?
There are many different ways to install node.js on a computer. The simplest method to verify whether node.js has been properly installed in your computer is simply type node-v in the command prompt or Windows PowerShell. But many times, it happens, most commonly if you're a beginner, the command prompt prints the output something like this: 'node'
2 min read
Top 3 Best Packages Of Node.js that you should try being a Node.js Developer
Node.js is an open-source and server-side platform built on Google Chrome's JavaScript Engine (V8 Engine). Node.js has its own package manager called NPM( Node Package Manager) which has very useful and incredible libraries and frameworks that makes our life easier as a developer to work with Node.js. The 3 Best Packages of Node.js that you should
4 min read