t3: accessing static files server side
I'm trying to use
fs.readFileSync
to read PDFs and font files (.ttf
) on the server (to dynamically build a PDF report in response to an API call), but I get ENOENT: no such file or directory
no matter what I do. Even when the static file is in the same directory as the code trying to access it, I can't seem to target it. I noticed that __dirname
targets /.next/server/app/api/trpc/[trpc]
rather than the server-side file system, so I haven't been able to use it to help solve this problem. I feel like I must be missing something obvious, but I haven't found the answer yet.Solution:Jump to solution
try using
process.cwd()
instead of __dirname
reference: https://github.com/vercel/next.js/discussions/14341
in which dir have you stored those pdf and font files...GitHub
__dirname pointing to
/
(during dev) · vercel next.js · Discussio...I'm trying to port some code to Next, and I'd say I was pretty familiar with how everything works, but this has stumped me (and there doesn't seem to be a mention in these discussions) ...
2 Replies
Solution
try using
process.cwd()
instead of __dirname
reference: https://github.com/vercel/next.js/discussions/14341
in which dir have you stored those pdf and font files
i am assuming those files are stored inside your dir on build time as nextjs on vercel only allows temporary / ephemeral storageGitHub
__dirname pointing to
/
(during dev) · vercel next.js · Discussio...I'm trying to port some code to Next, and I'd say I was pretty familiar with how everything works, but this has stumped me (and there doesn't seem to be a mention in these discussions) ...
process.cwd()
was the fix, thanks! I'm now able to use code like these examples to reference files in the /public
directory as well as files in /src/server
.