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)
);
}
2 Replies
JulieCezar
JulieCezar2y 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
pradeep
pradeepOP2y ago
thank you for answering really appreciate it
Want results from more Discord servers?
Add your server