-
Lets say i had two files in the root of my next.js project: // vercel.json
{
"functions": {
"src/pages/api/function.js": {
"includeFiles": "somedir/random2.txt"
}
}
} But no luck. |
Beta Was this translation helpful? Give feedback.
Replies: 11 comments 18 replies
-
Can you share your project code? There's zero configuration necessary for this. |
Beta Was this translation helpful? Give feedback.
-
Anyone else stumble upon this? Here is something you should try out, especially if you are reading a local file for an api call.
Vercel deployments are serverless and to ensure that at run time the path is correct, please use the following:
**assuming your file |
Beta Was this translation helpful? Give feedback.
-
@Timer here's a sample NextJS project I can share with you (https://github.com/gendronb/next-bullmq). The project works perfectly locally. My goal is to include .LUA files from node_modules (required by BullMQ, see taskforcesh/bullmq#469 (comment)), but in the sample code provide, I simply try to include some files located at the project root. When I deploy to Vercel and I inspect the deployment output, these files are not present. I did try many variations of vercel.json, with no luck. |
Beta Was this translation helpful? Give feedback.
-
I have the same issue. Things work locally but trigger an internal server error on Vercel. Any help would be greatly appreciated 😊 |
Beta Was this translation helpful? Give feedback.
-
I am having the exact same issue, I have a private key file in the root directory that an API function requires. The function works fine locally. However inspecting the vercel built output the private key file is nowhere to be see. I appreciate loading a private key file in this way is insecure...... I am switching to including the contents via an env variable. I just wanted to provide another example. |
Beta Was this translation helpful? Give feedback.
-
Any news about this? Also having the same problem |
Beta Was this translation helpful? Give feedback.
-
surprisingly difficult to do basic file reading... Came here after seeing this deceptively simple (not working) answer:
https://stackoverflow.com/questions/63066985/send-file-as-response-using-nextjs-api Make sure you install the correct version of next described here: #8251 (comment) |
Beta Was this translation helpful? Give feedback.
-
Why not to put all included files into just one folder? Try:
{
"functions": {
"pages/api/function.js": {
"includeFiles": "someDir/*.txt"
}
}
} Where pattern
// ... rest of handler
const txtFile1 = fs.readFileSync(
path.resolve(process.cwd(), 'someDir/random.txt'),
{ encoding: 'utf-8' }
)
const txtFile2 = fs.readFileSync(
path.resolve(process.cwd(), 'someDir/random2.txt'),
{ encoding: 'utf-8' }
) If you wanna get access to those files in other routes handlers too, you can try this (for all):
{
"functions": {
"page/api/*.js": {
"includeFiles": "someDir/*.txt"
}
}
} Just use glob patterns. Also these testers may help: It's super important to use If you have multiple functions rules then order matters. |
Beta Was this translation helpful? Give feedback.
-
What worked for me was to copy what you need to the assets folder with a custom For my usecase I needed to compile and type-check typescript code dynamically in the backend but the files I needed were not available in run-time so I added them to the assets folder. The typescript compiler goes looking for some lib files in the node_modules folder, so to circumvent that I use a virtual filesystem and voila. |
Beta Was this translation helpful? Give feedback.
-
same here |
Beta Was this translation helpful? Give feedback.
-
I updated our guide with more documentation and examples here for different frameworks. Hope this helps! https://vercel.com/guides/how-can-i-use-files-in-serverless-functions |
Beta Was this translation helpful? Give feedback.
I updated our guide with more documentation and examples here for different frameworks. Hope this helps!
https://vercel.com/guides/how-can-i-use-files-in-serverless-functions