How to Read Data From a File in Node.js

In NodeJS, to read data from an a file, you can use a quick and simple method in the “fs” module.

How to Read Data From a File in Node.js

//load the fs module
var fs = require('fs');

fs.readFile('/path/file.txt', "utf-8", function (err, data) {
  if(!err) {
    console.log("file successfully saved.");
  } else {
    console.log("error", err);
  }
});