How to Read and Write Files in Next.js on a Vercel Deployed Website

Nitish Kumar Singh

Dec 19, 2023 . 3 min read

Hello developers! In this blog post, we will explore how to read and write files in a Next.js server component or route handler in the production environment deployed on Vercel.

Photo by Clément Hélardot on Unsplash

This is what I learned when I was building a route handler for automatically updating the sitemap of my blog website, such as adding new URLs, updating the lastmod of existing URLs, and deleting a URL. In the development of this route handler in Next.js, I have clearly understood the answers to the following questions.

  • Can we read a file in the production environment?: Yes, we can read files in both the development and production environments, but the referencing paths are different in both environments. In my case, the path ./public/sitemap.xml works fine to refer to the sitemap file under the public folder in development, but it does not work in production. To refer to files in production, we need to use the process.cwd() function to get the current working directory (root directory) and then append the relative path of the file. In my case, to refer to the sitemap file, the path is process.cwd() + "/public/sitemap.xml".
  • Can we write a file in the production environment?: No, we can't write a file in the production environment because Vercel uses a serverless function (not a traditional server) to serve files that run on a read-only system. In my case, I successfully built and tested my route handler in development, but when I deployed it, I got EROFS: read-only file system, open './public/sitemap.xml'. However, Vercel provides a /tmp directory to write temporary files in Serverless Functions. You can explore more about it in GitHub Discussion and Vercel Guides.

We can read a file using process.cwd() to refer to the path and readFile() from the fs library to read files. The following code is an example of a Route Handler to read a JSON file from the app/data directory.

// A route handler in app/api/readJSON
import { promises as fs } from 'fs';

export async function GET(req) {
  try {
    const { searchParams } = new URL(req.url);
    let name = searchParams.get("fileName");
    const data = await fs.readFile(process.cwd() + '/app/' + name, 'utf8');
    return Response.json({ ok: true, results: data });
  } catch (error) {
    return Response.json({ ok: false, results: { title: "Failed reading JSON", message: error.message } });
  }
}

We can create a temporary file in the /tmp directory in Vercel's Serverless Functions; you can explore a full example in Vercel Guides. Visit Node.js and W3Schools to learn more about the fs (File System) Node.js library.

I hope you learn something by reading this blog post. Happy Coding!

Published on Dec 19, 2023
Comments (undefined)

Read More

How to Convert a DOM Element to JSON and from JSON to a DOM Element

Hello developers! In this blog post, we will create two JavaScript functions to convert a DOM element into a specific JSON format and, conversely, from JSON to a DOM element.

Dec 14, 2023

What is URL, Component, Route and Router in Web Development

I will discuss what a URL is, its parts and types, components and their types, routes and their types, and routers in web development. Understanding these concepts makes it easier for us to learn single-page application (SPA) libraries and frameworks because they are crucial in SPA development.

Dec 12, 2023

Is Vercel Server Running Down

In this blog post, I am going to discuss a problem that I have been facing for the last 20 days. The issue is related to hosting my websites on Vercel, built with Next.js. When I try to open my website, it does not load, and the browser displays the error: “This site can’t be reached: the website took too long to respond.”

Dec 11, 2023

A Beginner's Guide to Using and Setting Up Room Database in Android with Java

Hello developers! In this blog post, we will learn and understand Room Database in Android using Java, including how to set up and use Room Database to store data in local storage in an Android App.

Dec 9, 2023

Understanding Inline, Internal, and External CSS and Their Priority Order

Hello developers! In this blog post, we will explore Inline, Internal, and External CSS in web development and understand their priority order. For example, if all three types of CSS are applied to an element, then which CSS is effective.

Dec 8, 2023

Four Ways to Remove All Children from an HTML Element in JavaScript

Hello, developers! In this blog post, we will explore 4 methods for removing all children, including elements, texts, and comments, from an element in JavaScript.

Dec 7, 2023