fs.readFile()

Asynchronously reads the entire contents of a file. Callback receives an error and data, or use the promise-based version.

Syntax

nodejs
fs.readFile(path, options, callback)
fs.promises.readFile(path, options)

Example

nodejs
// Callback style:
fs.readFile("data.txt", "utf8", (err, data) => {
  if (err) throw err;
  console.log(data);
});

// Promise style:
const data = await fs.promises.readFile("data.txt", "utf8");
console.log(data);