1const fs = require('fs')
2
3try {
4 const data = fs.readFileSync('/Users/joe/test.txt', 'utf8')
5 console.log(data)
6} catch (err) {
7 console.error(err)
8}
1const fs = require('fs');
2
3fs.readFile('/Users/joe/test.txt', 'utf8' , (err, data) => {
4 if (err) {
5 console.error(err);
6 return
7 }
8 console.log(data);
9});
1// macOS, Linux, and Windows
2fs.readFileSync('<directory>');
3// => [Error: EISDIR: illegal operation on a directory, read <directory>]
4
5// FreeBSD
6fs.readFileSync('<directory>'); // => <data>
1fs.readFile('filename', function read(err, data) {
2 if (err) {
3 throw err;
4 }
5 var content = data;
6
7 console.log(content);
8
9});