Read a file one line at a time in node.js.
Read a file one line at a time in node.js?
328
29-Apr-2023
Updated on 01-May-2023
Aryan Kumar
01-May-2023To 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:
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.