1const fs = require('fs');
2
3fs.readFile('file.txt', function(err, data) {
4 if(err) throw err;
5
6 const arr = data.toString().replace(/\r\n/g,'\n').split('\n');
7
8 for(let i of arr) {
9 console.log(i);
10 }
11});
12
1const readline = require('readline');
2
3const readInterface = readline.createInterface({
4 input: fs.createReadStream('name.txt'),
5 output: process.stdout,
6 console: false
7 });
8
9 for await (const line of readInterface) {
10 console.log(line);
11 }
12//or
13readInterface.on('line', function(line) {
14 console.log(line);
15});
1var lineReader = require('line-reader');
2
3lineReader.eachLine('file.txt', function(line, last) {
4 console.log(line);
5 // do whatever you want with line...
6 if(last){
7 // or check if it's the last one
8 }
9});
10
1lineReader.open('file.txt', function(reader) {
2 if (reader.hasNextLine()) {
3 reader.nextLine(function(line) {
4 console.log(line);
5 });
6 }
7});
8