---
title: How can I use files in Vercel Functions?
description: Learn how to import files inside Serverless Functions on Vercel.
url: /kb/guide/how-can-i-use-files-in-serverless-functions
canonical_url: "https://vercel.com/kb/guide/how-can-i-use-files-in-serverless-functions"
published: 2025-11-03
last_updated: 2025-11-10
authors: Lee Robinson
related:
  - /docs/monorepos
  - /docs/storage/vercel-blob
install_vercel_plugin: npx plugins add vercel/vercel-plugin
---
<!-- docsgraph:related -->
## Related pages

> **For AI agents:** Follow these links to understand how this page connects to the rest of the Vercel ecosystem. For the full cross-link map (inbound, outbound, prerequisites, and semantic neighbors), see the .graph.md link below.

- [Supported Frameworks](https://vercel.com/docs/frameworks?from=related) — Vercel supports a wide range of the most popular frameworks, optimizing how your application builds and runs no matter w
- [Frontends](https://vercel.com/docs/frameworks/frontend?from=related) — Vercel supports a wide range of the most popular frontend frameworks, optimizing how your application builds and runs no
- [Full-stack](https://vercel.com/docs/frameworks/full-stack?from=related) — Vercel supports a wide range of the most popular backend frameworks, optimizing how your application builds and runs no
- [All Frameworks](https://vercel.com/docs/frameworks/more-frameworks?from=related) — Learn about the frameworks that can be deployed to Vercel.
- [Node.js](https://vercel.com/docs/functions/runtimes/node-js?from=related) — Learn how to use the Node.js runtime to create functions and deploy Node.js servers on Vercel.
- [How can I use AWS S3 with Vercel?](https://vercel.com/kb/guide/how-can-i-use-aws-s3-with-vercel?from=related) — Example how to use AWS S3 library on Vercel
- [How to Load Data from a File in Next.js](https://vercel.com/kb/guide/loading-static-file-nextjs-api-route?from=related) — Learn how to display and read the contents of a static json file in your Next.js application.
- [Using Express.js with Vercel](https://vercel.com/kb/guide/using-express-with-vercel?from=related) — Learn how to use Express.js in a Serverless environment.
- [Build with Vercel Blob on Nuxt](https://vercel.com/kb/guide/vercel-blob-nuxt?from=related) — Set up Vercel Blob in a Nuxt application with NuxtHub, upload and serve files, and deliver optimized images with Nuxt Im
- [Troubleshooting Build Error: "Serverless Function has exceeded the unzipped maximum size of 250 MB"](https://vercel.com/kb/guide/troubleshooting-function-250mb-limit?from=related) — Learn how to troubleshoot builds failing due to exceeding the maximum function size limit on Vercel.

Full cross-link map for this page: [/kb/guide/how-can-i-use-files-in-serverless-functions.graph.md](/kb/guide/how-can-i-use-files-in-serverless-functions.graph.md)
<!-- /docsgraph:related -->


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.

## How bundling application code works

Your application gets bundled during a build to include all necessary user code and dependencies needed for runtime.

Both Next.js and general Vercel Functions use Vercel’s [Node File Trace](https://github.com/vercel/nft) 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.

## Examples of reading files

### Using `process.cwd()`

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.

```javascript
import fs from 'fs';
import path from 'path';

export function GET(request) {
  let usersPath = path.join(process.cwd(), 'users.json');
  let file = fs.readFileSync(usersPath);
  return new Response(file);
}
```

### 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:

```javascript
import { createRequire } from 'node:module';

let sayHello = createRequire(import.meta.url)('../greet.cjs');

export function GET(request) {
  return new Response(sayHello());
}
```

This function `api/file.js` is requiring `greet.cjs`, which is in the root of the repository.

```javascript
function sayHello() {
  return 'Hello, World!';
}

module.exports = sayHello;
```

To test this locally, ensure your `package.json` is configured for ES Modules:

```json
{
  "type": "module"
}
```

Finally, to tell Vercel to include the `greet.cjs` file while bundling, modify `vercel.json`:

```json
{
  "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](https://nextjs.org/docs/pages/api-reference/next-config-js/output#caveats) of the framework to include additional files rather than `vercel.json`. The file path can be a [glob](https://www.npmjs.com/package/minimatch) to select multiple files.

Notably, if you are trying to read files from `node_modules`, you will need to add this to `outputFileTracingIncludes`.

```javascript
module.exports = {
  experimental: {
    outputFileTracingIncludes: {
      '/api/another': ['./necessary-folder/**/*'],
    },
  },
}
```

Further, sometimes if you are using a [monorepo](https://vercel.com/docs/monorepos), you’ll have a different root directory for your application. To include files outside of that folder with Next.js, you can use:

```javascript
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.

```javascript
import { read } from '$app/server';
import users from './users.json';

export async function load() {
  return {
    users: await read(users).text()
  };
}
```

### Using Astro

Astro uses Node File Trace and also supports the ability to [include](https://docs.astro.build/en/guides/integrations-guide/vercel/#includefiles) or [exclude](https://docs.astro.build/en/guides/integrations-guide/vercel/#excludefiles) files. You do not need to modify `vercel.json` with this approach.

```javascript
import { defineConfig } from 'astro/config';
import vercel from '@astrojs/vercel/serverless';

export default defineConfig({
  output: 'server',
  adapter: vercel({
    includeFiles: ['./users.json'],
  }),
});
```

### Using Nuxt

Nuxt can use [server assets](https://github.com/pi0/nuxt-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](https://nitro.unjs.io/guide/storage) API.

```javascript
export default defineEventHandler(async () => {
  // https://nitro.unjs.io/guide/assets#server-assets
  const assets = useStorage('assets:server')
  const users = await assets.getItem('users.json')
  return {
    users
  }
})
```

## Examples of writing files

If you are looking for a way to write files, we recommend persisting to object storage like [Vercel Blob](https://vercel.com/docs/storage/vercel-blob) or similar solutions.