> ## Documentation Index
> Fetch the complete documentation index at: https://bun.zhcndoc.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 在 Vercel 上部署 Bun 应用

[Vercel](https://vercel.com/) 是一个云平台，允许你构建、部署和扩展你的应用。

<Warning>
  Bun 运行时目前处于 Beta 阶段；某些功能（例如自动源码映射、字节码缓存、在
  `node:http/https` 上的指标）尚不支持。
</Warning>

<Note>
  `Bun.serve` 当前不支持在 Vercel Functions 上使用。请在 Vercel 支持的框架中使用 Bun，如 Next.js、
  Express、Hono 或 Nitro。
</Note>

***

<Steps>
  <Step title="在 vercel.json 中配置 Bun">
    要启用 Bun 运行时以支持你的 Functions，在 `vercel.json` 文件中添加 `bunVersion` 字段：

    ```json vercel.json icon="file-json" theme={"theme":{"light":"github-light","dark":"dracula"}}
    {
    	"bunVersion": "1.x" // [!code ++]
    }
    ```

    Vercel 会自动检测此配置，并在 Bun 上运行你的应用。该值必须是 `"1.x"`，Vercel 会内部管理具体的次要版本号。

    为了达到最佳效果，请确保你的本地 Bun 版本与 Vercel 使用的版本相匹配。
  </Step>

  <Step title="Next.js 配置">
    如果你要部署一个 **Next.js** 项目（包括 ISR），请更新 `package.json` 中的脚本，使其使用 Bun 运行时：

    ```json package.json icon="file-json" theme={"theme":{"light":"github-light","dark":"dracula"}}
    {
    	"scripts": {
    		"dev": "bun --bun next dev", // [!code ++]
    		"build": "bun --bun next build" // [!code ++]
    	}
    }
    ```

    <Note>
      `--bun` 标志会在 Bun 下运行 Next.js CLI。打包（通过 Turbopack 或 Webpack）保持不变，但所有命令均在 Bun 运行时内执行。
    </Note>

    这可确保本地开发和构建都使用 Bun。
  </Step>

  <Step title="部署你的应用">
    将你的代码库连接至 Vercel，或通过 CLI 进行部署：

    ```bash terminal icon="terminal" theme={"theme":{"light":"github-light","dark":"dracula"}}
    # 使用 bunx（无全局安装）
    bunx vercel login
    bunx vercel deploy
    ```

    或者全局安装 Vercel CLI：

    ```bash terminal icon="terminal" theme={"theme":{"light":"github-light","dark":"dracula"}}
    bun i -g vercel
    vercel login
    vercel deploy
    ```

    [查看更多 Vercel 部署 CLI 文档 →](https://vercel.com/docs/cli/deploy)
  </Step>

  <Step title="验证运行时">
    要确认你的部署使用了 Bun，可以打印 Bun 版本：

    ```ts index.ts icon="https://mintcdn.com/bun-zhcndoc/cnUTwgMuf4cCrwC-/icons/typescript.svg?fit=max&auto=format&n=cnUTwgMuf4cCrwC-&q=85&s=e7767043c9e885c34f2d6c8fe2a95217" theme={"theme":{"light":"github-light","dark":"dracula"}}
    console.log("runtime", process.versions.bun);
    ```

    ```txt theme={"theme":{"light":"github-light","dark":"dracula"}}
    runtime 1.3.3
    ```

    [参考 Vercel Bun 运行时文档了解功能支持 →](https://vercel.com/docs/functions/runtimes/bun#feature-support)
  </Step>
</Steps>

***

* [Fluid 计算](https://vercel.com/docs/fluid-compute)：Bun 和 Node.js 运行时均运行在 Fluid 计算上，并支持相同的核心 Vercel Functions 功能。
* [中间件](https://vercel.com/docs/routing-middleware)：若要使用 Bun 运行路由中间件，需要将运行时设置为 `nodejs`：

```ts middleware.ts icon="https://mintcdn.com/bun-zhcndoc/cnUTwgMuf4cCrwC-/icons/typescript.svg?fit=max&auto=format&n=cnUTwgMuf4cCrwC-&q=85&s=e7767043c9e885c34f2d6c8fe2a95217" theme={"theme":{"light":"github-light","dark":"dracula"}}
export const config = { runtime: "nodejs" }; // [!code ++]
```
