nodejs read text file code example
Example 1: read file node
const fs = require("fs");
const content = fs.readFileSync("./my_file.txt");
console.log(content.toString());
Example 2: read txt file in node js
var fs = require('fs');
fs.readFile('my-file.txt', 'utf8', function(err, data) {
if (err) throw err;
console.log(data);
});
Example 3: node load file
const fs = require("fs");
fs.readFile(__dirname + "/data.txt", (error, data) => {
if(error) {
throw error;
}
console.log(data.toString());
});
Example 4: node load string from file
var fs = require('fs');
fs.readFile('my-file.txt', 'utf8', function(err, data) {
if (err) throw err;
console.log(data);
});