How can I use files in Vercel Functions?
Knowledge BaseGuides
Learn how to import files inside Serverless Functions on Vercel.
What sets us apart?
Your application gets bundled during a build to include all necessary user code and dependencies needed for runtime.
3 min read
This guide will explain how to read files from Vercel Functions, both when used with frameworks like Next.js or standalone on Vercel. We’ll explain how bundling works, how you can tell Vercel to include additional files for use at runtime in your functions, and more.
3 Nov 2025
Examples of reading files
Using process.cwd()
How bundling application code works
We recommend using process.cwd() to determine the current directory of the Vercel Function instead of using __dirname. For example, this function reads the file users.json from the root of the repository.
Add as preferred
Both Next.js and general Vercel Functions use Vercel’s Node File Trace to determine which files (including those in node_modules) are necessary to be included. This uses static analysis to inspect any import, require, and fs usage and determine all files that a page might load.
1
importfsfrom'fs';
2
importpathfrom'path';
4
exportfunctionGET(request){
5
let usersPath = path.join(process.cwd(),'users.json');
6
let file = fs.readFileSync(usersPath);
7
returnnewResponse(file);
8 }
Using dynamic require
If you are trying to write your code using ES Modules, sometimes you might rely on CommonJS code. To dynamically require and include a function using CJS, you can write a function as follows:
1
import{ createRequire }from'node:module';
To test this locally, ensure your package.json is configured for ES Modules:
"functions":{
"api/file.js":{
"includeFiles":"greet.cjs"
Using Next.js
Since Next.js has its own build process which uses Node File Trace, you would use the built-in functionality of the framework to include additional files rather than vercel.json. The file path can be a glob to select multiple files.
Notably, if you are trying to read files from node_modules, you will need to add this to outputFileTracingIncludes.
module.exports={
experimental:{
outputFileTracingIncludes:{
'/api/another':['./necessary-folder/**/*'],
},
},
}
Further, sometimes if you are using a monorepo, you’ll have a different root directory for your application. To include files outside of that folder with Next.js, you can use:
module.exports={
experimental:{
// includes files from the monorepo base two directories up
outputFileTracingRoot: path.join(__dirname,'../../'),
},
}
Using SvelteKit
SvelteKit uses Node File Trace and also supports the ability to read files. You do not need to modify vercel.json with this approach.
1
import{ read }from'$app/server';
2
importusersfrom'./users.json';
4
exportasyncfunctionload(){
5
return{
6
users:awaitread(users).text()
7
};
8 }
Using Astro
Astro uses Node File Trace and also supports the ability to include or exclude files. You do not need to modify vercel.json with this approach.
1
import{ defineConfig }from'astro/config';
4
exportdefaultdefineConfig({
2
importvercelfrom'@astrojs/vercel/serverless';
8
}),
9 });
6
adapter:vercel({
Using Nuxt
7
includeFiles:['./users.json'],
Nuxt can use server assets to include files into your Vercel Function. Any file inside server/assets/ is by default included. You can access server assets using storage API.
return{
users
const assets =useStorage('assets:server')
If you are looking for a way to write files, we recommend persisting to object storage like Vercel Blob or similar solutions.
// https://nitro.unjs.io/guide/assets#server-assets
const users =await assets.getItem('users.json')
})
Related documentation
}
Examples of writing files