> ## 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.

# 使用 Next.js 和 Bun 构建应用

[Next.js](https://nextjs.org/) 是一个用于构建全栈 Web 应用的 React 框架。它支持服务器端渲染、静态站点生成、API 路由等功能。Bun 提供快速的包安装，并且可以运行 Next.js 的开发和生产服务器。

***

<Steps>
  <Step title="创建一个新的 Next.js 应用">
    使用交互式 CLI 创建一个新的 Next.js 应用。这将搭建一个新的 Next.js 项目并自动安装依赖。

    ```sh terminal icon="terminal" theme={"theme":{"light":"github-light","dark":"dracula"}}
    bun create next-app@latest my-bun-app
    ```
  </Step>

  <Step title="启动开发服务器">
    切换到项目目录，使用 Bun 启动开发服务器。

    ```sh terminal icon="terminal" theme={"theme":{"light":"github-light","dark":"dracula"}}
    cd my-bun-app
    bun --bun run dev
    ```

    这将使用 Bun 的运行时启动 Next.js 开发服务器。

    使用浏览器打开 [`http://localhost:3000`](http://localhost:3000) 查看效果。对 `app/page.tsx` 的任何更改都会在浏览器中热更新。
  </Step>

  <Step title="更新 package.json 中的脚本">
    修改 `package.json` 中的 scripts 字段，在 Next.js CLI 命令前加上 `bun --bun`。这确保 Bun 来执行 Next.js CLI，用于常见任务如 `dev`、`build` 和 `start`。

    ```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 ++]
        "start": "bun --bun next start", // [!code ++]
      }
    }
    ```
  </Step>
</Steps>

***

## 部署托管

基于 Bun 的 Next.js 应用可以部署到多种平台。

<Columns cols={3}>
  <Card title="Vercel" href="/guides/deployment/vercel" icon="https://mintcdn.com/bun-zhcndoc/cnUTwgMuf4cCrwC-/icons/ecosystem/vercel.svg?fit=max&auto=format&n=cnUTwgMuf4cCrwC-&q=85&s=e92543e72c0e2d1ee325c8c8cf295557" width="24" height="24" data-path="icons/ecosystem/vercel.svg">
    部署到 Vercel
  </Card>

  <Card title="Railway" href="/guides/deployment/railway" icon="https://mintcdn.com/bun-zhcndoc/cnUTwgMuf4cCrwC-/icons/ecosystem/railway.svg?fit=max&auto=format&n=cnUTwgMuf4cCrwC-&q=85&s=6d4ced01059256dc15e14daaab430783" width="24" height="24" data-path="icons/ecosystem/railway.svg">
    部署到 Railway
  </Card>

  <Card title="DigitalOcean" href="/guides/deployment/digital-ocean" icon="https://mintcdn.com/bun-zhcndoc/cnUTwgMuf4cCrwC-/icons/ecosystem/digitalocean.svg?fit=max&auto=format&n=cnUTwgMuf4cCrwC-&q=85&s=a1693d18e3518f25e10a2d3572d8a02d" width="24" height="24" data-path="icons/ecosystem/digitalocean.svg">
    部署到 DigitalOcean
  </Card>

  <Card title="AWS Lambda" href="/guides/deployment/aws-lambda" icon="https://mintcdn.com/bun-zhcndoc/cnUTwgMuf4cCrwC-/icons/ecosystem/aws.svg?fit=max&auto=format&n=cnUTwgMuf4cCrwC-&q=85&s=76b4d37ef79ff74ede5a0d58562736a5" width="24" height="24" data-path="icons/ecosystem/aws.svg">
    部署到 AWS Lambda
  </Card>

  <Card title="Google Cloud Run" href="/guides/deployment/google-cloud-run" icon="https://mintcdn.com/bun-zhcndoc/7hwCkUCcx3ux5DPj/icons/ecosystem/gcp.svg?fit=max&auto=format&n=7hwCkUCcx3ux5DPj&q=85&s=717edc2f17132055927a2f48361ebb55" width="24" height="24" data-path="icons/ecosystem/gcp.svg">
    部署到 Google Cloud Run
  </Card>

  <Card title="Render" href="/guides/deployment/render" icon="https://mintcdn.com/bun-zhcndoc/cnUTwgMuf4cCrwC-/icons/ecosystem/render.svg?fit=max&auto=format&n=cnUTwgMuf4cCrwC-&q=85&s=b0bb39432bfc1ee783888859611d9898" width="24" height="24" data-path="icons/ecosystem/render.svg">
    部署到 Render
  </Card>
</Columns>

***

## 模板

<Columns cols={2}>
  <Card title="Bun + Next.js 基础入门" img="https://mintcdn.com/bun-zhcndoc/7hwCkUCcx3ux5DPj/images/templates/bun-nextjs-basic.png?fit=max&auto=format&n=7hwCkUCcx3ux5DPj&q=85&s=cb27618705fb0de55ebba97a5bc7dfed" href="https://github.com/bun-templates/bun-nextjs-basic" arrow="true" cta="前往模板" width="2212" height="1326" data-path="images/templates/bun-nextjs-basic.png">
    一个简单的 App Router 入门模板，包含 Bun、Next.js 和 Tailwind CSS。
  </Card>

  <Card title="基于 Next.js + Bun 的待办事项应用" img="https://mintcdn.com/bun-zhcndoc/7hwCkUCcx3ux5DPj/images/templates/bun-nextjs-todo.png?fit=max&auto=format&n=7hwCkUCcx3ux5DPj&q=85&s=fe6696118e9559de8785151fa5e8cfcd" href="https://github.com/bun-templates/bun-nextjs-todo" arrow="true" cta="前往模板" width="2212" height="1326" data-path="images/templates/bun-nextjs-todo.png">
    一个全栈待办事项应用，使用 Bun、Next.js 以及 PostgreSQL 构建。
  </Card>
</Columns>

***

[→ 查看 Next.js 官方文档](https://nextjs.org/docs) 获取更多有关构建和部署 Next.js 应用的信息。
