Unable to upload files after deploying on vercel

This is how I implemented it but, does not seem to work as I get an error stating that no such file or directory '/vercel'
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.join(__filename);;

const directoryPath = path.resolve(__dirname, 'public');
const body = await req.formData()

const file = body.get("file") as File;

const buffer = Buffer.from(await file.arrayBuffer());
const relativeUploadDir = `/uploads`

const uploadDir = path.join(process.cwd(), relativeUploadDir);

console.log(uploadDir)
console.log(file.type)

try {
await stat(uploadDir);
} catch (e: any) {
if (e.code === "ENOENT") {
// This is for checking the directory is exist (ENOENT : Error No Entry)
await mkdir(uploadDir, { recursive: true });
} else {
console.error(
"Error while trying to create directory when uploading a file\n",
e
);
return NextResponse.json(
{ error: "Something went wrong." },
{ status: 500 }
);
}
}
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.join(__filename);;

const directoryPath = path.resolve(__dirname, 'public');
const body = await req.formData()

const file = body.get("file") as File;

const buffer = Buffer.from(await file.arrayBuffer());
const relativeUploadDir = `/uploads`

const uploadDir = path.join(process.cwd(), relativeUploadDir);

console.log(uploadDir)
console.log(file.type)

try {
await stat(uploadDir);
} catch (e: any) {
if (e.code === "ENOENT") {
// This is for checking the directory is exist (ENOENT : Error No Entry)
await mkdir(uploadDir, { recursive: true });
} else {
console.error(
"Error while trying to create directory when uploading a file\n",
e
);
return NextResponse.json(
{ error: "Something went wrong." },
{ status: 500 }
);
}
}
1 Reply
JulieCezar
JulieCezar2mo ago
Vercel doesn't work like this. You cannot use their FS to store something on the server, because all functions are transformed into Lambdas, which die after they are done. So you would be losing your data anyways. If you want to store files somewhere you need another solution for this. E.g. UploadThing that Theo and crew made, AWS S3, MinIO or w/e else you like
Want results from more Discord servers?
Add your server