DEV Community

Cover image for Node.js : Determining the line count of a text file
Rajesh Kumar Yadav
Rajesh Kumar Yadav Subscriber

Posted on • Edited on

Node.js : Determining the line count of a text file

Create a new file as app.js and paste below code -

app.js

 const readline = require('readline'); const fs = require('fs'); var file = 'path.to.file'; var linesCount = 0; var rl = readline.createInterface({ input: fs.createReadStream(file), output: process.stdout, terminal: false }); rl.on('line', function (line) { linesCount++; // on each linebreak, add +1 to 'linesCount' }); rl.on('close', function () { console.log(linesCount); // print the result when the 'close' event is called }); 
Enter fullscreen mode Exit fullscreen mode

Run below command -

 node app.js 
Enter fullscreen mode Exit fullscreen mode

You will get the line count of text file.

Buy Me A Coffee

With all that being said, I highly recommend you keep learning!

Thank you for reading this article. Please feel free to connect with me on LinkedIn and Twitter.

Top comments (0)