A Complete Guide for Reading Files in Node.js

From the title, we clearly know that there are 3 ways to read files in Node. They are known as the promise way, the callback way, and the synchronous way. The Promise way enables us to use the modern…

image

Photo by Drew Coffman on Unsplash

In this post, we will be learning about

  • Three ways to read a file in Node.js
  • Which is my favorite way out of the 3?
  • Why did I choose it as my favorite way? Without further ado, let’s get started.

Three ways for File Reading in Node.js

From the title, we clearly know that there are 3 ways to read files in Node. They are known as the promise way, the callback way, and the synchronous way. Let’s take a look into the promise way.

Read File with the Promise way

The Promise way enables us to use the modern asynchronous Javascript feature — async and await. Refer to the sample code below. Running the code above will give you such output.

Start reading file
Running other operation
content <Buffer ef bb bf 22 6c 6f 73 74 22 2c 22 6b 6e 69 66 65 22 2c 22 70 61 63 6b 22 2c 22 63 61 6c 6c 22 2c 22 74 61 6c 65 73 22 2c 22 70 72 6f 64 75 63 74 69 6f ... 1721522 more bytes>
Finished reading file
Read File with the Callback way

The approach for Callback way is to use fs.readFile Node API. This method gave us the callback when the file is completely read into memory. Let’s see it in the below code sample. Running the code above will give you a similar output as well.

Start reading file
Running other operation
content <Buffer ef bb bf 22 6c 6f 73 74 22 2c 22 6b 6e 69 66 65 22 2c 22 70 61 63 6b 22 2c 22 63 61 6c 6c 22 2c 22 74 61 6c 65 73 22 2c 22 70 72 6f 64 75 63 74 69 6f ... 1721522 more bytes>
Finished reading file
Read File with the Synchronous way

The synchronous way is implemented using the fs.readFileSync Node API. Running the code above will give you the below output.

Start reading file
content <Buffer ef bb bf 22 6c 6f 73 74 22 2c 22 6b 6e 69 66 65 22 2c 22 70 61 63 6b 22 2c 22 63 61 6c 6c 22 2c 22 74 61 6c 65 73 22 2c 22 70 72 6f 64 75 63 74 69 6f ... 1721522 more bytes>
Finished reading file
Running other operation

One thing worth mentioning here is the synchronous way will block the next line execution until the file is successfully read. As you can see above, the Runnint other operation log has only shown after Finished reading file .

Which is my favorite way and why?

The Promise Way My top favorite way is the promise way. There are 2 factors that act as my consideration in my favorite selection process:

  • Code Readability & Popularity
  • Blocking vs Non-Blocking
Code Readability & Popularity

From the popularity perspective, async and await is the modern and popular way of writing asynchronous code in the JavaScript world. As the codebase grows, I believe we will see more usage of async and await instead of the callback way. From the code readability perspective, the promise way definitely looks cleaner and has better readability as well compared to the callback way. By using async and await , the flow to read the code is pretty smooth from top to down.

Blocking vs Non-Blocking

Both The Promise Way and The Callback Way offer better efficiency to the whole system since it does not block the code while reading the file. While the synchronous way will block the code for execution until the file is read into memory.

Conclusion

In this guide, we explored the different ways to read a file in Node.js. Besides, I also choose my own favorite way and based on the factor of code readability and popularity, and the blocking vs non-blocking code. However, do bear in mind we did not consider the performance of each different approach in this guide. I will probably do this in the next post. If performance is the primary concern, do check out my upcoming post. Thanks for reading and I hope you found this helpful.

Enjoyed this article?

Share it with your network to help others discover it

Continue Learning

Discover more articles on similar topics