Using the Bun Runtime with Vercel Functions
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.
For all frameworks, including Next.js, you can configure the runtime in your vercel.json file using the bunVersion property:
{
"$schema": "https://openapi.vercel.sh/vercel.json",
"bunVersion": "1.x"
}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"
}
}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.
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 fetch work with both runtimes.
See the table below for a detailed comparison:
Feature | Bun Runtime | Node.js Runtime |
|---|---|---|
| Node.js APIs | ||
| Automatic source maps | ||
| Request metrics (node:http/https) | ||
| Request metrics (fetch) |
Vercel Functions using the Bun runtime support most Node.js APIs, including standard Web APIs such as the Request and Response Objects.
Bun has built-in TypeScript support with zero configuration required. The runtime supports files ending with .ts inside of the /api directory as TypeScript files to compile and serve when deploying.
export default {
async fetch(request: Request) {
const url = new URL(request.url);
const name = url.searchParams.get('name') || 'World';
return Response.json({ message: `Hello ${name}!` });
},
};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.
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.
Was this helpful?