Next.js Discord

Discord Forum

Read file from a Route Handler in Vercel

Unanswered
Cuban Crocodile posted this in #help-forum
Open in Discord
Cuban CrocodileOP
Hello!

Is there a way to read a file from within a Route Handler configured with "force-dynamic" once a Next.js app is deployed to Vercel? I'm unable to remove that config in the actual project where I experienced the issue. The below is a minimal representation of what works well locally but not in Vercel.

import { promises as fs } from "fs";

import { NextResponse } from "next/server";
import { globby } from "globby";

export const dynamic = "force-dynamic";

export const GET = async () => {
  const file = await fs
    .readFile(process.cwd() + "/src/app/some-file.txt", "utf8")
    .catch(() => "FILE NOT LOADED");

  const files = await globby("**/*", {
    ignore: ["**/node_modules"],
  });

  if (file === "FILE NOT LOADED") {
    return new NextResponse(JSON.stringify(files, null, 2));
  }

  return new NextResponse(file);
};


And here is the response from the call to globby

[
  "___next_launcher.cjs",
  "package.json"
]


Without the "force-dynamic" config, everything works as expected with all the files of the entire project visible.

Any ideas?

0 Replies