Phong Duong

🔎Search

Read a file in Nodejs

18-03-2021 | #nodejs

fs module provides readFile method that allows you to read the file's content.

const fs = require("fs")

fs.readFile("path/to/file", (error, data) => {
  if (error) {
    return console.log(error);
  }
  
  console.log(data);
})

The callback receives 2 arguments: error and data. error will be null and data will contain the file's content if there is no error. Else, error contains the error message.

Share: TwitterFacebookLinkedInHacker News