Writing and Reading Files Using NodeJS in NextJS App Router

janac
3 min readFeb 10, 2024

JavaScript is used on the frontend and backend.

Does the guy’s right hand only have 4 fingers? Source — Bing Image Creator

Frontend: Read files, no writing.

On the frontend, we can read files when the user uploads them, but we can’t write files programmatically — a user would have to initiate a download to write files from a website to their computer. This makes sense from a security stand point — imagine if a website could just start writing files on to your computer as soon as you visited it, without clicking on anything. Viruses and keyloggers would be every where.

This confused me at first since there are File APIs in JavaScript that are accessible in the frontend (File API — Web APIs | MDN (mozilla.org)). However, these are all for reading and manipulating uploaded files.

Backend: Read and write files.

Using NodeJS, we have utilities for writing files.

  1. fs
  2. fs-promise

Both are similar, but you will most likely need to use fs-promise which returns a promise as its result, versus fs which takes a callback.

In this article we will focus on fs-promise.

fs promise

--

--

janac

Most of my writing is about software. I enjoy summarizing and analyzing books and self-help videos. I am senior software consultant at LazerTechnologies.com.