Node.js Read File

The fs module can be used to read the contents of a file.

Example

const fs = require("fs");

const data = fs.readFileSync(
    "example.txt",
    "utf8"
);

console.log(data);
        
Output:

Hello from Node.js!

Explanation

  1. Import the fs module.
  2. Use readFileSync().
  3. Give the file name.
  4. Use utf8 to read text.
  5. Display the file content.
← Back to Home