fs node read 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: nodejs readfile
const fs = require('fs');
fs.readFile('/Users/joe/test.txt', 'utf8' , (err, data) => {
if (err) {
console.error(err);
return
}
console.log(data);
});
Example 3: fs.readfile
fs.readFile('filename', function read(err, data) {
if (err) {
throw err;
}
var content = data;
console.log(content);
});
Example 4: javascript fs read
fs.readFile('/etc/passwd', (err, data) => {
if (err) throw err;
console.log(data);
});
Example 5: node read file sync
fs.readFileSync('<directory>');
fs.readFileSync('<directory>');
Example 6: fs readfile encoding
fs.readFile('/etc/passwd', 'utf8', callback);