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
- Import the fs module.
- Use readFileSync().
- Give the file name.
- Use utf8 to read text.
- Display the file content.
↠Back to Home