---
title: "Read a file one line at a time in node.js?"  
description: "Read a file one line at a time in node.js?"  
author: "Revati S Misra"  
published: 2023-04-29  
updated: 2023-05-01  
canonical: https://www.mindstick.com/forum/158083/read-a-file-one-line-at-a-time-in-node-js  
category: "node.js"  
tags: ["javascript", "node js"]  
reading_time: 1 minute  

---

# Read a file one line at a time in node.js?

Read a [file](https://www.mindstick.com/articles/59/encrypting-and-decrypting-files-using-c-sharp) [one line](https://www.mindstick.com/forum/159096/how-can-i-run-two-commands-in-one-line-in-windows-cmd) at a time in node.js.

## Replies

### Reply by Aryan Kumar

To read a file one line at a time in Node.js, you can use the **createReadStream** function from the built-in **fs** module along with the **readline** module. Here is an example:

```javascript
const fs = require('fs');
const readline = require('readline');
const readInterface = readline.createInterface({
 input: fs.createReadStream('path/to/file.txt'),
 output: process.stdout,
 console: false
});
readInterface.on('line', function(line) {
 console.log(line);
});
```

In this example, we create a **readInterface** object using the **createInterface** function, which takes an input stream and an output stream as arguments. We pass in a read stream created with **fs.createReadStream**, specifying the path to the file we want to read.

We then listen for the **line** event on the **readInterface** object, which is emitted each time a new line is read from the file. In the event handler, we simply log the line to the console, but you could also perform any other desired operations on each line.

\


---

Original Source: https://www.mindstick.com/forum/158083/read-a-file-one-line-at-a-time-in-node-js

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
