Writing/reading from the Vercel file system

This topic has been discussed in the past and I can’t find a solution for this (Writing/reading from the Vercel file system)

I have a route that is trying to load a .sql query to pass to the postgres driver. I am following the examples laid out here: How can I use files in Vercel Functions?

I see that in the Source tab the .sql file is present there but I keep getting ‘file not found’.

In a server route, can’t I read a sql file? or any file for that matter? Or do I need a plugin for this use case?

Thank you

Hi @webfountain, your server route should be able to read files (but can’t write files to system storage because an invocation is short lived).

Can you share a little bit more about your project? What is the tech stack you are using?

Also, about the .sql file, can you check the deployment summary to see if the file was included in the build?

@anshumanb Good to know. As of now I am not writing to files but just trying to read sql files.

Tech stack is the default that comes with installing npx create-next-app@latest. The route.js file has a single GET method and it is from there that I cam calling into the database using the sql file that the postgres driver accepts.

In the deployment summary, do you mean in the ‘Static Assests’ section? I do not see it in that section. I do see in the Build Logs that the database is connected and the sql file is read (presumably to pre-render?)

Thanks for sharing additional details. Can you share the code where you read the file? (including any imports)

About deployment summary, sometimes the files are not included in static assets but still bundled in the serverless functions. I think your assumptions about Build logs is correct.

In the meantime, you can explicitly tell Next.js to include the files using outputFileTracingRoot option in the config.

This is essentially the code:

import fs from ‘fs’;
import path from ‘path’;

const getFile = (sqlPath: string): string => {
const fullPath = path.join(process.cwd(), sqlPath);

let file = fs.readFileSync(fullPath, ‘utf8’);
console.log(‘file’, file);

return file;
}

I reduced it to exactly this example. This is being called inside a server action with the 'use server' annotation.

I will try using outputFileTracingRoot and see if that works.

Hi @webfountain, thanks for confirming.

This seems related to Program in server actions cannot bundle the correct static file - #5 by anshumanb. And if I remember correctly we had released a fix for it.

@anshumanb outputFileTracingIncludes worked!

I am running nextjs latest and I’m still not getting the static files. So not too sure the ‘fix’ worked in all cases. But adding outputFileTracingIncludes does the trick. Thank you!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.