Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,939 questions

51,876 answers

573 users

How to read file line by line in Node.js

1 Answer

0 votes
const readline = require('readline');
const fs = require('fs');

const rl = readline.createInterface({
    input: fs.createReadStream('log.js')
});

let lineNumber = 0;
rl.on('line', textLine => {
    lineNumber++;
    console.log(`Line: ${lineNumber} ${textLine}`);
});

rl.on('close', () => {
    console.log(`\nTotal lines: ${lineNumber}`);
});


    
/*
run:
  
Line: 1 // The module: log.js
Line: 2 
Line: 3 const events = require('events');
Line: 4 
Line: 5 class Log extends events {
Line: 6    show(s) {
Line: 7      console.log(s);
Line: 8   
Line: 9      // Fire (Raise) an event with arguments:
Line: 10      this.emit('gonodego', { id: 88622, name: 'Elizabeth' });
Line: 11   }
Line: 12 }
Line: 13 
Line: 14 module.exports = Log;

Total lines: 14

*/

 



answered Mar 22, 2020 by avibootz

Related questions

1 answer 201 views
201 views asked Mar 22, 2020 by avibootz
1 answer 170 views
170 views asked Mar 9, 2020 by avibootz
1 answer 139 views
...