Read files in API route, fails on Vercel

I have a simple API request that reads some dirs and files and creates a response out of it. In the Vercel's docs, I read that I have to use process.cwd() but that does not help.
// api/presets.get.ts
import fs from 'node:fs/promises'
import path from 'node:path'

export default defineEventHandler(async (event) => {
const dir = path.join(process.cwd(), 'public', 'presets', 'front', 'us')
const files = await fs.readdir(dir)

return files
})
// api/presets.get.ts
import fs from 'node:fs/promises'
import path from 'node:path'

export default defineEventHandler(async (event) => {
const dir = path.join(process.cwd(), 'public', 'presets', 'front', 'us')
const files = await fs.readdir(dir)

return files
})
Previously, I had presets inside of a server directory, but that did not work on Vercel, so I moved it to the public. Obviously, hoped it would work, but it's the same. It cannot find the directory. I also read somewhere that Nuxt supports useStorage() to have server assets, but I do not think that's what I need here, or is it?
3 Replies
Nenad Novakovic
Nenad NovakovicOP6mo ago
process.cwd() returns:
[
"chunks",
"index.mjs",
"index.mjs.map",
"node_modules",
"package.json"
]
[
"chunks",
"index.mjs",
"index.mjs.map",
"node_modules",
"package.json"
]
So no wonder it cannot read from public dir. How to make it read from public directory tho? Seems like it's unresolvable. Spent all day trying to figure it out with different approaches and nothing worked. Had to rewrite all the logic and use Vercel's Postgres + Drizzle.
Sam  K
Sam K6mo ago
is it normal ssr or vercel edge? In my case whenever I need to read files in Nuxt/Nitro I use useStorage as it provides better typed and also to not enter the loophole to where are my files 😂. this is nito's docs on useStorage https://nitro.unjs.io/guide/assets
Nenad Novakovic
Nenad NovakovicOP6mo ago
Yea, I tried that before the rewrite too. But it was not convenient for me, because one of my endpoints indeed needed to go through all as directories were parameters and filenames were ids. If you know what I mean... And with this hook, it's not not possible to list all, without knowing the path and filename.
Want results from more Discord servers?
Add your server