Read and Write in Next Js Is not working in production (vercel)

this code is working in local environment but not working in production can anyone tell me how to resolve this issue?
import { NextResponse } from "next/server";
import { promises as fs } from "fs";
import path from "path";
type dbSchema = {
pageView: number;
printResume: number;
savePdf: number;
};
export async function GET() {
const jsonDirectory = path.join(process.cwd(), "json");
const data = await fs.readFile(process.cwd() + "/public/db.json", "utf-8");
const dataInJson = JSON.parse(data);
return NextResponse.json(dataInJson);
}
export async function POST(req: Request) {
const data = (await req.json()) as { type: "print" | "view" | "save" };
let db = await ReadDb();
const dbJson = JSON.parse(db) as dbSchema;
if (data.type === "print") {
dbJson.printResume = dbJson.printResume + 1;
} else if (data.type === "save") {
dbJson.savePdf = dbJson.savePdf + 1;
} else if (data.type === "view") {
dbJson.pageView = dbJson.pageView + 1;
}
SaveDb(dbJson);
return NextResponse.json({ status: "Ok" });
}
async function ReadDb() {
const jsonDirectory = path.join(process.cwd(), "json");
const db = await fs.readFile(process.cwd() + "/public/db.json", "utf-8");

return db;
}
async function SaveDb(newDb: dbSchema) {
const db = await fs.writeFile(
process.cwd() + "/public/db.json",
JSON.stringify(newDb)
);
}
import { NextResponse } from "next/server";
import { promises as fs } from "fs";
import path from "path";
type dbSchema = {
pageView: number;
printResume: number;
savePdf: number;
};
export async function GET() {
const jsonDirectory = path.join(process.cwd(), "json");
const data = await fs.readFile(process.cwd() + "/public/db.json", "utf-8");
const dataInJson = JSON.parse(data);
return NextResponse.json(dataInJson);
}
export async function POST(req: Request) {
const data = (await req.json()) as { type: "print" | "view" | "save" };
let db = await ReadDb();
const dbJson = JSON.parse(db) as dbSchema;
if (data.type === "print") {
dbJson.printResume = dbJson.printResume + 1;
} else if (data.type === "save") {
dbJson.savePdf = dbJson.savePdf + 1;
} else if (data.type === "view") {
dbJson.pageView = dbJson.pageView + 1;
}
SaveDb(dbJson);
return NextResponse.json({ status: "Ok" });
}
async function ReadDb() {
const jsonDirectory = path.join(process.cwd(), "json");
const db = await fs.readFile(process.cwd() + "/public/db.json", "utf-8");

return db;
}
async function SaveDb(newDb: dbSchema) {
const db = await fs.writeFile(
process.cwd() + "/public/db.json",
JSON.stringify(newDb)
);
}
J
JulieCezar322d ago
You can't do that on Vercel Because the backend is deployed on Lambdas meaning you don't actually have a server all the time And even if you write something to FS it will be deleted when the lambda is down and different lambdas will not have acess to the same FS Any kind of FS operation shouldnt be on Vercel
P
pradeep322d ago
thank you for answering really appreciate it
Want results from more Discord servers?
Add your server
More Posts
Desperate for help with Trpc resolver.How can i get resolve({ default: base64Data }); to only trigger once trpc has returned success or fuseRouter queryHow do y'all handle typescript with nextjs's query params? Currently I'm doing ```const cartId = quPassword hashing on EDGE?I'm making an app and noticing that vercel's serverless functions are taking very much time to complAuth.js Muti-device SupportI'm implementing auth right now in a site that the user will likely be switching from mobile to deskNextJS should I use a headless CMS?Hello I am working on a project that is a multi user blog where anyone can create a post and commenHow to define this exact relation on prismaGiven this two models, how could I implement a relation between them, where a duel has 2 user Ids inHow much do y'all charge for your apps?I have created two complete custom apps for clients, took 40-60 hours a piece. Just curious how y'aDrizzle client side vs server side?hi can someone help explain to me a bit about drizzle? as far as i can tell, its doing client side at3-turbo + next-i18n + tRPC doesn't work on Vercel only locallyHi. I tried to implement server side localisation in tRPC according to this article https://levelupAnyone have an exampe of two nextjs applications in a single repo?Basically I want to have a website for the customer facing site and another for the admin portal to Recommendations for validation library?in t3 docs under other recommendations section, there is no validation library there https://createsuperplanet conection fails in production modeI need help with next auth / planetscale connection. my app works fine in localhost but does not conQuestion about next auth.Hey all, The stack and full stack development in javascript in general is new for me. I finding my Why does `npm create t3-app@latest` use the /pages directory. How can I use the app router?I want to build an application which uses the latest nextjs app router, however the `create t3-app@lnextjs13 do get request with searchbarHi, I am currently building a nextjs 13 project, trying to get in touch with the app router and servHow to sent a mutation procedure from the clientupdateUserStats:protectedProcedure .input(z.object({ id:z.string().nonempty(), p