Next.js Discord

Discord Forum

Vercel can't find local json file

Answered
Morelet’s Crocodile posted this in #help-forum
Open in Discord
Morelet’s CrocodileOP
I'm using the app router. My data is stored in /app/data/data.json. My server function to access this file looks like this and it works fine locally just not when deployed to Vercel.

"use server"
import { promises as fs } from 'fs';
import { Thing } from './types';

export default async function getData(): Promise<Thing[]> {
  const file = await fs.readFile(process.cwd() + '/app/data/data.json', 'utf8');
  const data = JSON.parse(file);

  return data
}


I also changed my next.config.mjs to trace this file but seems to be of no help.

/** @type {import('next').NextConfig} */
const nextConfig = {
  experimental: {
    outputFileTracingIncludes: {
      "/api/": ["/app/data/**/*"],
    },
  },
};

export default nextConfig;


Getting the error
Error: ENOENT: no such file or directory, open '/var/task/app/data/data.json'
    at async open (node:internal/fs/promises:636:25)
Answered by joulev
import data from "./data.json"
View full answer

6 Replies

Answer
no need to use fs
@joulev `import data from "./data.json"`
Morelet’s CrocodileOP
wow lmao that easy. was i just misunderstanding all the guides and stuff about using local data? swear they all said to use that fs stuff and process.cwd
@joulev `import data from "./data.json"`
Morelet’s CrocodileOP
thanks a ton by the way
@Morelet’s Crocodile wow lmao that easy. was i just misunderstanding all the guides and stuff about using local data? swear they all said to use that fs stuff and process.cwd
i don't know honestly... people probably think they have to use fs because they are reading a file. however importing json is supported natively by typescript; importing in javascript is possible too with a bit more markup