Using the Bun Runtime with Vercel Functions
Using the Bun Runtime with Vercel Functions
The Bun runtime is available in Beta on all plans.
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 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, which contains several breaking changes. Set this version explicitly once you have migrated your application."1.x"selects the previous latest Bun version (currently1.3.14).
{
"$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:
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.
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:
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, you must change your build and dev commands in your package.json file to use the Bun runtime:
Before:
{
"scripts": {
"dev": "next dev",
"build": "next build"
}
}
After:
{
"scripts": {
"dev": "bun run --bun next dev",
"build": "bun run --bun next build"
}
}
Routing Middleware
The Bun runtime works with Routing Middleware 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 with uncompressed bundles up to 5 GB and extended max duration up to 30 minutes. Both features are in beta.
| Feature | Bun Runtime | Node.js Runtime |
|---|---|---|
| Node.js APIs | ||
| Fluid compute | ||
| Active CPU | ||
| Streaming | ||
waitUntil |
||
| Logs | ||
| Automatic source maps | ||
| Bytecode caching | ||
| Request metrics (node:http/https) | ||
| Request metrics (fetch) |
Supported APIs
Vercel Functions using the Bun runtime support most Node.js APIs, including standard Web APIs such as the Request and Response Objects.
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:httpornode:httpsmodules
Both runtimes run on Fluid compute and support the same core Vercel Functions features.