# Using the Bun Runtime with Vercel Functions

The Bun runtime is available in [Beta](/content/docs/release-phases#beta/index.html) on [all plans](/content/docs/plans/index.html).

Bun is a fast, all-in-one JavaScript runtime that serves as an alternative to Node.js.

Bun provides Node.js API compatibility and is generally faster than Node.js for CPU-bound tasks. It includes a bundler, test runner, and package manager.

## Configuring the runtime

For all frameworks, including Next.js, you can configure the runtime in your `vercel.json` file using the [`bunVersion`](/content/docs/project-configuration/vercel-json#bunversion/index.html) property.

Once you configure the runtime version, Vercel manages the patch versions automatically. Currently, `"1.4.x"` and `"1.x"` are the only valid versions:

- `"1.4.x"` selects the [complete rewrite of Bun from Zig to Rust](https://bun.com/blog/bun-v1.4), which contains several [breaking changes](https://bun.com/blog/bun-v1.4#upgrading-to-1-4). Set this version explicitly once you have migrated your application.
- `"1.x"` selects the previous latest Bun version (currently `1.3.14`).

```json
{
  "$schema": "https://openapi.vercel.sh/vercel.json",
  "bunVersion": "1.4.x"
}
```

Vercel manages the Bun minor versions automatically. `1.4.x` and `1.x` are the only valid values currently.

## Deploy with the Bun framework preset

Use the Bun framework preset when you want one `Bun.serve()` server to route requests for your application. After you configure `bunVersion` in `vercel.json`, add a `bun.lock` file and a `server` entrypoint in the project root or the `src/` directory:

- `server.{js,cjs,mjs,ts,cts,mts}`
- `src/server.{js,cjs,mjs,ts,cts,mts}`

With Bun 1.2 or later, run `bun install` to create `bun.lock`. For older versions, run `bun install --save-text-lockfile`. The preset doesn't detect the legacy `bun.lockb` format.

A minimal project contains `package.json`, `bun.lock`, `server.ts`, and the `vercel.json` configuration shown above. You don't need an `/api` directory or routing configuration.

Call `Bun.serve()` once during module startup. Vercel uses that call to detect the server, then routes incoming requests through a Vercel Function. You can use the `fetch`, `routes`, `error`, and `websocket` options:

```typescript
Bun.serve({
  routes: {
    '/health': () => Response.json({ status: 'ok' }),
  },
  fetch() {
    return new Response('Hello from Bun on Vercel');
  },
});
```

The `port` and `hostname` options only apply when you run the server locally. They don't configure the public endpoint on Vercel. Unix sockets and HTML imports in `routes` are not supported.

To serve WebSocket connections with `Bun.serve()`, see the [Bun example in the WebSockets documentation](/content/docs/functions/websockets#bun/index.html).

## Deploy a Bun server from `/api`

Create `api/server.ts` to deploy a native Bun server as one Vercel Function. Vercel serves the function at `/api/server`, so you can add it to a project that also contains a frontend.

To use custom routing with an `/api` server, configure route overrides in `vercel.json`. Each override must use the full request path, including the `/api/server` prefix.

Call `Bun.serve()` once during module startup:

```typescript
Bun.serve({
  fetch(request) {
    const url = new URL(request.url);

return Response.json({
      message: 'Hello from Bun on Vercel',
      pathname: url.pathname,
    });
  },
});
```

This deployment model only requires the `bunVersion` configuration shown above. It doesn't use the Bun framework preset or require `bun.lock`. Unlike the preset, it only sends requests for `/api/server` to this server.

## Framework-specific considerations

### Next.js

When using Next.js, and [ISR](/content/docs/incremental-static-regeneration/index.html), you must change your `build` and `dev` commands in your package.json file to use the Bun runtime:

Before:

```json
{
  "scripts": {
    "dev": "next dev",
    "build": "next build"
  }
}
```

After:

```json
{
  "scripts": {
    "dev": "bun run --bun next dev",
    "build": "bun run --bun next build"
  }
}
```

### Routing Middleware

The Bun runtime works with [Routing Middleware](/content/docs/routing-middleware/index.html) the same way as the Node.js runtime once you set the `bunVersion` in your `vercel.json` file. Note that you'll also have to set the runtime config to `nodejs` in your `middleware.ts` file.

## Feature support

The Bun runtime on Vercel supports most Node.js features. The main differences relate to automatic source maps, bytecode caching, and request metrics on the `node:http` and `node:https` modules. Request metrics using the `fetch` work with both runtimes.

Vercel Functions using the Bun runtime support [large functions](/content/docs/functions/limitations#large-functions-beta/index.html) with uncompressed bundles up to 5 GB and [extended max duration](/content/docs/functions/configuring-functions/duration#extended-max-duration-beta/index.html) up to 30 minutes. Both features are in beta.

| Feature | Bun Runtime | Node.js Runtime |
| --- | --- | --- |
| Node.js APIs |  |  |
| [Fluid compute](/content/docs/fluid-compute/index.html) |  |  |
| [Active CPU](/content/docs/functions/usage-and-pricing#active-cpu/index.html) |  |  |
| [Streaming](/content/docs/functions/streaming-functions/index.html) |  |  |
| [`waitUntil`](/content/docs/functions/functions-api-reference/vercel-functions-package#waituntil/index.html) |  |  |
| [Logs](/content/docs/functions/logs/index.html) |  |  |
| Automatic source maps |  |  |
| [Bytecode caching](/content/docs/fluid-compute#bytecode-caching/index.html) |  |  |
| Request metrics (node:http/https) |  |  |
| Request metrics (fetch) |  |  |

## Supported APIs

Vercel Functions using the Bun runtime support [most Node.js APIs](https://bun.sh/docs/runtime/nodejs-apis), including standard Web APIs such as the [Request and Response Objects](/content/docs/functions/runtimes/node-js#node.js-request-and-response-objects/index.html).

## Performance considerations

Bun is generally faster than Node.js, especially for CPU-bound tasks. Performance varies by workload, and in some cases Node.js may be faster depending on the specific operations your function performs.

## When to use Bun

Bun is best suited for new workloads where you want a fast, all-in-one toolkit with built-in support for TypeScript, JSX, and modern JavaScript features. Consider using Bun when:

- You want faster execution for CPU-bound tasks
- You prefer zero-config TypeScript and JSX support
- You're starting a new project and want to use modern tooling

Consider using Node.js instead if:

- Node.js is already installed on your project and is working for you
- You need automatic source maps for debugging
- You need request metrics on the `node:http` or `node:https` modules

Both runtimes run on [Fluid compute](/content/docs/fluid-compute/index.html) and support the same core Vercel Functions features.
