read file sync nodejs code example
Example 1: readfilesync
const fs = require('fs')
try {
const data = fs.readFileSync('/Users/joe/test.txt', 'utf8')
console.log(data)
} catch (err) {
console.error(err)
}
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: node read file sync
fs.readFileSync('<directory>');
fs.readFileSync('<directory>');
Example 4: fs readfile encoding
fs.readFile('/etc/passwd', 'utf8', callback);