Next.js Discord

Discord Forum

can't write files with Vercel

Unanswered
Seppala Siberian Sleddog posted this in #help-forum
Open in Discord
Seppala Siberian SleddogOP
I have an API endpoint that get's a file buffer and write it into a file before sending the file to Google Cloud, whenever i deploy it to vercel i get an error
error: ENOENT: no such file or directory, open './tmp/audio/stt/73995211-0a58-4f7f-b504-598110393f67.mp3'

and that's the code for writing the file in the API endpoint
// text_to_speech.ts
 const id = uuidv4();
 // destructuring the fileBuffer key from the body
 const { fileBuffer } = req.body;

 if (!fileBuffer) throw new Error('File not found');

 fs.writeFileSync(`./tmp/${id}.mp3`, fileBuffer, {
        encoding: 'binary',
 });

13 Replies

European sprat
You don't get access to the filesystem with Vercel. Is there a reason why you need to write to the filesystem before sending to Google?
Seppala Siberian SleddogOP
thanks for the help i have realised that i don't really need to save the file i can send the buffer directly to google cloud, but it would be nice to know how to use the file system in Vercel for other use cases.
@Seppala Siberian Sleddog I have tried this approach as well it does not work unfortunately.
Actually it should work. As long as you write to /tmp it will work
But you cannot write to any directories outside /tmp on vercel
@joulev Actually it should work. As long as you write to /tmp it will work
Seppala Siberian SleddogOP
as you can see i'm writing to tmp directory i'm very suriprised why it does not work
@Seppala Siberian Sleddog as you can see i'm writing to `tmp` directory i'm very suriprised why it does not work
You are writing to ./tmp not /tmp that’s why it doesn’t work. They are different places
Seppala Siberian SleddogOP
I have added this line for testing purposes
const file = writeFileSync('/tmp/helloworld.txt', 'hello world', 'utf8');

and i get this error
Error: ENOENT: no such file or directory, open '/tmp/helloworld.txt'
@Seppala Siberian Sleddog I have added this line for testing purposes ts const file = writeFileSync('/tmp/helloworld.txt', 'hello world', 'utf8'); and i get this error Error: ENOENT: no such file or directory, open '/tmp/helloworld.txt'
this works for me though :thinkies:
import { writeFileSync } from "fs";

export const dynamic = "force-dynamic";

export async function GET(): Promise<Response> {
  try {
    writeFileSync("/tmp/helloworld.txt", "hello world", "utf8");
    return new Response("Ok!");
  } catch (e) {
    return new Response(String(e), { status: 500 });
  }
}

* https://github.com/joulev/debug/tree/write-to-tmp-vercel
* https://debug-git-write-to-tmp-vercel-joulev.vercel.app/
@Seppala Siberian Sleddog are you using the AppRouter?
app/pages shouldn't matter in this case, but yes
@joulev app/pages shouldn't matter in this case, but yes
Seppala Siberian SleddogOP
that's weird because the exact same code does not work with me, but thanks for the help though.