The easiest way to read a file in Node is to use the `fs.readFile()` function to asynchronously reads the entire contents of a file. Alternatively, you can use the synchronous version `fs.readFileSync()`

The easiest way to read a file in Node is to use the fs.readFile() function to asynchronously reads the entire contents of a file. The first argument it takes is the path of the file. The second one is the options that could be an object with two properties encoding and flag or the options could be a string that specifies the encoding, if the encoding is not specified the the raw buffer is returned. The last argument is a callback function that has the structure errback that will be called with two arguments (err, data), where data is the contents of the file.

const fs = require('fs');

fs.readFile('test.txt', (err, data) => {
    if (err) throw err;
    console.log(data);
});

or

const fs = require('fs');

fs.readFile('test.txt', 'utf8', (err, data) => {
    if (err) throw err;
    console.log(data);
});

The default encoding is utf8, but you can specify a custom encoding using a a second parameter.

Alternatively, you can use the synchronous version fs.readFileSync(), it is just like the fs.readFile(), but it only takes the first 2 parameters and doesn’t require a callback, it the returns the content of the file.

const fs = require('fs');

const data = fs.readFileSync('test.txt', 'utf8');

console.log(data);

Both functions fs.readFile() and fs.readFileSync() buffers the enterie file, that is to say, they read the full content of the file in memory before returning the data. That means that bigger files are going to have a major impact on your memory consumption. In this case, a better option is to read the file content using streams vi fs.createReadStream() to minimize memory costs.

for more information check out fs