Using fs and node.js to read information from a plain text file.

Gregory Ybos
4 min readJun 14, 2021
Photo by Sigmund on Unsplash

It should be “plain” to see the importance of our JavaScript document's ability to process information dynamically with minimal need for manual manipulation or editing of data beforehand. This becomes even more critical as the amount of information increases. The file system (fs) module in conjunction with the readline module for node.js allows us to stream information from plain text to our JavaScript document and negates the need to manually copy/paste the information into a format our JavaScript document and read and understand.

To start we will need to tell Node.js that we will require the fs and readline modules for our code. Both of these modules are included with Node.js so no further action is needed to provide these modules to our JavaScript document. The keyword “require” is used to tell node.js to include these modules at run time. We initialize these modules to constant variables as we will not be reassigning them within our document.

Following this, we will create an asynchronous function called processLineByLine which will take a given file path (in the form of a string) to the plain text document we want to read. Using the fs module’s createReadStream method on this file path we create a “stream” of information from the plain text document which we will assign to a constant variable fileStream.

We can not use the stream of information in this form. I think of a stream and a non-stop flow of information from the document which we need to control. This is where the readline module comes in handy. By using readline’s createInterface method we will be able to loop through the streamline by line and manipulate the line to meet our purposes. This method takes an argument of an object with a key/value pair of input: fileStream.

Next comes the fun part. We will be looping over each line of the supplied stream and modifying it in some way. One option would be to push each line into the index of an array or maybe assign them to key/value pairs in an object. In this example, we will simply return the output of our loop as a new string broken by newlines. This loop also needs to be asynchronous so we will use the keyword await. We will be taking each line as a template literal and adding a new line break to the end and add each new line to an output string. We must make sure to return our output at the end of our function!

Seems pointless to return the information in the same format we received it, right? In my next blog, we will get into parsing this information into a data structure JavaScript can use in a meaningful way. For now, we have accomplished our goal from bring in information from a plain text document and giving our JavaScript document the opportunity to process it in some way using Node.js and associated modules. All that is left to do is call this function with a file path to a plain text file and verify the output. To do this we will call our function with a string value of the path to the plain text file we would like to read from. Below this, we will use the keyword ‘then’ to return the result of the async function’s promise and log it to the console.

--

--