For enquiries call:

Phone

+1-469-442-0620

HomeBlogWeb DevelopmentHow to read files with Node.js?

How to read files with Node.js?

Published
05th Sep, 2023
Views
view count loader
Read it in
5 Mins
In this article
    How to read files with Node.js?

    In this tutorial, we will explore different ways we can read files in Node.js.  

    In software development, reading files is a regular part of the job. However, this simple process very often becomes complicated and tedious. Fortunately, that's not the case in Node.js. Node.js provides a built-infile system(fs) module, to carry out file operations. 

    Before we start, let us make sure we have the basic prerequisites. You will need to ensure you have: 

    • Node.js  
    • Code Editor (Ex. VS Code, Atom) 

    For this tutorial, my environment setup includes: 

    • Node.js - v12.16.1 
    • Code Editor - VS Code 

    fs module 

    fs module is a built-in module in the Node.js. You do notnecessarily need to install it.  

    The fs module supports both Synchronous and Asynchronous function calls. 

    Getting started 

    I am assuming you have installed the NodeJS. 

    • Create a new directory node-fs.  
    • Create 2 files inside the node-fs, data.txt and index.js
    • Paste the dummy data in the data.txt.

    Lorem ipsum dolor sit amet, consecteturadipiscingelit, sed do eiusmodtemporincididuntutlabore et dolore magna aliqua. 

    Open this directory in the code editor. 

    From the index.js, we will read the test.txt.  

    Read file Synchronously using readFileSync 

    The readFileSync is a synchronous method to read a file. 

    Syntax 

    fs.readFileSync(path[, options]) 

    • pathfilepath 
    • options 
      • encoding string or buffer Default null 
      • flag string Defaultr

    Open index.js in the Code Editor. 

    Paste the below code.

    // import the fs module
    const fs = require("fs");
    // read file synchronously
    constreadData = fs.readFileSync("./data.txt");
    console.log(readData);

    Open the terminal or cmd in the node-fs directory and run the below command.

    $ node index.js
    <Buffer4c 6f 72 65 6d 20 69 70 73 75 6d 20 64 6f 6c 6f 72 20 73 69 74 20 61 6d 65 74 2c 20 63 6f 6e 73 65 63 74 65 74 75 72 20 61 64 69 70 69 73 63 69 6e 67 ... 73 more bytes>

    It has returned the content in the buffer format. The readFileSync by default returns the content in the Buffer format.

    To get the content in normal string format, we need to pass the utf8 encoding as a parameter.

    Change the code.

    // import the fs module
    const fs = require("fs");
    // read file synchronously
    constreadData = fs.readFileSync("./data.txt", "utf8");
    console.log(readData);

    run it.

    $ node index.js
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

    Read file Asynchronously using readFile

    Asynchronous methods are great when you don’t want to wait for a process to complete.  
    fs module provides an asynchronous method to read a file. It returns a callback.
    Syntax

    fs.readFileSync(path[, options], callback)
    • pathfilepath
    • options
      • encoding string or buffer Default null
      • flag string Defaultr
    • callback function
      • err Error
      • data string | Buffer

    Syntax is similar tothe synchronous method except it returns a callback function.

    // import the fs module
    const fs = require("fs");
    // read file asynchronously
    fs.readFile("./data.txt", "utf8", (err, data) => {
        if (!err) {
            console.log(data);
        } else {
            console.error("Something went wrong while reading the file.");
        }
    });

    Run the code.

    $ node index.js
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

    Read file using Stream 

    Reading file using fs.readFileis an ideal way if the file size is less. If the size is huge, the read will consume the complete Memory and you may find your system hanging. 

    In such scenarios, Stream is the best option. This breaks the file into small chunks of data and sends a piece at a time. In this way, the system can continue with other tasks without allocating the complete memory to it.   

    Let us code and understand. 

    Node.js provides a createReadStreammethod to stream the file. createReadStreamreturns a fs.ReadStreamobject. The readStream events like data, end, or error. To read the file, we have to listen to these events.  

    • The data event will return the content of the file in chunks.  
    • The end event will notify that, there is no data left in the file to read. 
    • The error event returns an error. 
    // import the fs module 
    constfs = require("fs"); 
    // read file using stream 
    const streamRead = async () => { 
    return new Promise((resolve, reject) => { 
    try { 
    // create a read stream 
    const streamRead = fs.createReadStream("./data.txt", { 
    encoding:"utf8", 
          }); 
    // listen to data event 
    streamRead.on("data", (data) => { 
    console.log(data); 
          }); 
    // listen to end event 
    streamRead.on("end", () => resolve("File read complete"); 
          }); 
    // listen to error event 
    streamRead.on("error", (error) => { 
    throw Error"Something went wrong while streaming the file."error.message 
            ); 
          }); 
        } catch (error) { 
          reject(error); 
        } 
      }); 
    }; 
    streamRead().then((res, err) => { 
    if(!err) console.log(res); 
    else console.error(err); 
    }); 

    Run the code.

    $ node index.js
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
    File read complete

    Experiment time! 

    For the purpose of the experiment, I created a file of 20MB to check the performance of readFileSync, readFileand createReadStream 

    The average memory consumption of VS Code is 600-700MB in my machine. 

    This is a screenshot before running any of the commands. 

    How to read files with Node.js

    Looking to enhance your coding skills? Discover the best online Python course with certificate. Unlock endless possibilities and become a Python pro today!

    readFileSync 

    Check the screenshot. This operation took 373MB and it went on to upto ~450MB

    readFileSync

    readFile (Asynchronous)

    Check the screenshot. This operation took 506MB and it went on to upto ~600MB.

    readFile (Asynchronous)

    createReadStream 

    Check the screenshot. This operation took 287MB and it reached upto ~300MB.  

    createReadStream

    Conclusion 

    Node.js provides multiple methods to read a file. It completely depends on the file size and the requirement, which method to choose. From our performance tests, streaming turns out to be the most efficient method to use.  

    The performance may vary from machine to machine and file size. One must note that what we have demonstrated is just for the sake of a demo, so this must not be taken to be the final words. There are other npm packages available for file system, for example,fs-extra.  

    Hope this tutorial has helped. Taking a formal training course in Node.js would be a great way to fast-track your career.  

    Happy coding!

    Tired of fixing bugs? Find easy solutions to your programming queries with our live, interactive workshops. Explore now.

    Profile

    Shubham Chadokar

    Author

    I am a Software Engineer who loves to write articles and tutorials on the latest technologies. I write majorly on Nodejs and Golang. 

    Share This Article
    Ready to Master the Skills that Drive Your Career?

    Avail your free 1:1 mentorship session.

    Select
    Your Message (Optional)

    Upcoming Web Development Batches & Dates

    NameDateFeeKnow more
    Course advisor icon
    Course Advisor
    Whatsapp/Chat icon