---
title: How can I use files in Vercel Functions?
description: Learn how to import files inside Serverless Functions on Vercel.
url: "https://vercel.com/kb/guide/how-can-i-use-files-in-serverless-functions"
published: 2025-11-03
last_updated: 2026-09-28
authors: Lee Robinson
install_vercel_plugin: npx plugins add vercel/vercel-plugin
search_with_vercel_cli: npx vercel@latest kb search "<query>"
read_with_vercel_cli: "npx vercel@latest kb inspect how-can-i-use-files-in-serverless-functions"
---

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.

## Related Documentation

- [Vercel Functions](/docs/functions)
- [Next.js](/docs/frameworks/full-stack/nextjs)

## More Vercel Functions guides

- [How can I improve function cold start performance on Vercel?](/kb/guide/improve-function-cold-start-performance-on-vercel): Learn how to confirm whether cold starts cause function latency on Vercel, and how Fluid compute reduces how often they happen.
- [What is Vercel's policy regarding load testing deployments?](/kb/guide/what-s-vercel-s-policy-regarding-load-testing-deployments): Learn about Vercel's policies regarding load tests.
- [Hosting your API on Vercel](/kb/guide/hosting-backend-apis): Learn how to build and scale performant APIs on Vercel.

## Reference Architecture

Choose a stack and work through the decisions behind it, with curated documentation, guides, templates, and courses to help you build.

- [Agent stack](/kb/architecture/agents): Build and operate agents with models, tools, and durable workflows.
- [Web applications](/kb/architecture/web): Choose rendering, data, authentication, and caching approaches.
- [Backends](/kb/architecture/backends): Choose a runtime, connect services, and run background work.

[Browse all stacks](/kb/architecture)

## Build with prompts

Build an app, extend an agent, or improve your project with ready-to-use prompts for your coding agent.

[Browse the prompt library](/kb/prompts) · [Read the Markdown index](/kb/prompts.md)

## Search guides from your terminal

Search the Knowledge Base and read guides as Markdown with the Vercel CLI. No login required.

```bash
# Find guides by keyword
npx vercel@latest kb search "rewrites and redirects"
npx vercel@latest kb search "AI SDK streaming"

# Show the latest guides
npx vercel@latest kb

# Print a guide as Markdown, by slug or URL
npx vercel@latest kb inspect <guide>
```
