Open In App

Node.js Method

Last Updated : 03 Apr, 2023
Like Article

Node.js is used for server-side scripting. Reading and writing files are the two most important operations that are performed in any application. Node.js offers a wide range of inbuilt functionalities to perform read and write operations. The fs package contains the functions required for file operations. The read() method of the fs package reads the file using a file descriptor. In order to read files without file descriptors the readFile() method of the fs package can be used. 

Syntax, buffer, offset, length, position, callback)


  • fd: File descriptor returned by method.
  • buffer: Stores the data fetched from the file.
  • offset: Offset in the buffer indicating where to start writing.
  • length: An integer that specifies the number of bytes to read.
  • position: An integer that specifies where to begin reading from in the file. If the position is null, data is read from the current file position.
  • callback: The callback function accepts the three arguments ie. (err, bytesRead, buffer).

The method opens the file and returns a file descriptor. A file descriptor is a number or index that is stored in the file descriptor table by the kernel and is used to uniquely identify an open file in the computer’s operating system. The method reads the file using the file descriptor and stores it in the buffer. The contents of the buffer are printed out as output. The fs.close() method is used to close the file. 

Example 1: In this example, we will use method


const fs = require("fs");
const buffer = new Buffer.alloc(1024);
console.log("Open existing file");'gfg.txt', 'r+', function (err, fd) {
    if (err) {
        return console.error(err);
    console.log("Reading the file");, buffer, 0, buffer.length,
        0, function (err, bytes) {
            if (err) {
            if (bytes > 0) {
                    slice(0, bytes).toString());
            console.log(bytes + " bytes read");
            // Close the opened file.
            fs.close(fd, function (err) {
                if (err) {
                console.log("File closed successfully");


Open existing file
Reading the file
0 bytes read
File closed successfully

Example 2: Taking dynamic input for file name/path. 


// Module required to accept user
// input from console
const readline = require('readline-sync');
const fs = require("fs");
let path = readline.question("Enter file path: ");
console.log("Entered path : " + path);
console.log("File Content ");
fs.stat(path, function (error, stats) {
    // 'r' specifies read mode, "r", function (error, fd) {
        var buffer = new Buffer.alloc(stats.size);, buffer, 0, buffer.length,
            null, function (error, bytesRead, buffer) {
                var data = buffer.toString("utf8");


C:\Users\User\Desktop>node read3.js
Enter file path: new.txt
Entered path : new.txt
File Content
Hello World..
Welcome to GeeksForGeeks..
This is a Node.js program

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads