Node.js File System: Read from File Node.js

The Node.js file system module (fs module) allows you to work with the files.

The fs.readFile() is used to read from file.

There are two ways to read file

  1. Asynchronously
  2. Synchronously
1. Read file Asynchronously:
var fs = require('fs');
fs.readFile('my_file.txt', (err, data) => {
	if (err){
		throw err;
		console.log("Content of file is:  " + data);
2. Read file Synchronously:
var fs = require('fs');
var filename = 'my_file.txt';
var content = fs.readFileSync(filename);
console.log('Content of file is: ' + content);