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

# bun init

> 使用交互式命令 `bun init` 搭建一个空的 Bun 项目骨架

通过 `bun init` 搭建一个新项目，开始使用 Bun。

```bash terminal icon="terminal" theme={"theme":{"light":"github-light","dark":"dracula"}}
bun init my-app
```

```txt theme={"theme":{"light":"github-light","dark":"dracula"}}
? 选择一个项目模板 - 按回车键确认。
❯ 空白
  React
  库

✓ 选择的项目模板：空白

 + .gitignore
 + CLAUDE.md
 + .cursor/rules/use-bun-instead-of-node-vite-npm-pnpm.mdc -> CLAUDE.md
 + index.ts
 + tsconfig.json（用于编辑器自动补全）
 + README.md
```

按 `enter` 接受每个提示的默认答案，或者传递 `-y` 标志以自动接受默认值。

***

`bun init` 使用 Bun 生成一个新项目。它会用合理的默认值推断设置，并且在多次运行时不会破坏现有内容。

<Frame>
  ![演示](https://user-images.githubusercontent.com/709451/183006613-271960a3-ff22-4f7c-83f5-5e18f684c836.gif)
</Frame>

它会创建：

* 一个 `package.json` 文件，名称默认为当前目录名
* 一个 `tsconfig.json` 或 `jsconfig.json` 文件，取决于入口文件是否为 TypeScript 文件
* 一个入口文件，默认为 `index.ts`，除非存在任一 `index.{tsx, jsx, js, mts, mjs}` 文件，或 `package.json` 指定了 `module` 或 `main` 字段
* 一个 `README.md` 文件

AI Agent 规则（可通过设置环境变量 `$BUN_AGENT_RULE_DISABLED=1` 禁用）：

* 当检测到 Claude CLI 时，会创建一个 `CLAUDE.md` 文件（可通过环境变量 `CLAUDE_CODE_AGENT_RULE_DISABLED` 禁用）
* 当检测到 Cursor 时，会创建一个 `.cursor/rules/*.mdc` 文件，指导 [Cursor AI](https://cursor.sh) 使用 Bun 替代 Node.js 和 npm

如果传递 `-y` 或 `--yes`，则默认继续而不询问任何问题。

最后，会运行 `bun install` 来安装 `@types/bun`。

***

## CLI 用法

```bash terminal icon="terminal" theme={"theme":{"light":"github-light","dark":"dracula"}}
bun init <folder?>
```

### 初始化选项

<ParamField path="--yes" type="boolean">
  {" "}

  接受所有默认提示，无需询问。别名：<code>-y</code>{" "}
</ParamField>

<ParamField path="--minimal" type="boolean">
  {" "}

  仅初始化类型定义（跳过应用脚手架）。别名：<code>-m</code>{" "}
</ParamField>

### 项目模板

<ParamField path="--react" type="string|boolean">
  {" "}

  搭建一个 React 项目。无值时，创建一个基础的 React 应用。
  <br /> 支持以下预设值：{" "}

  <ul>
    {" "}

    <li>
      <code>tailwind</code> – 预配置了 Tailwind CSS 的 React 应用
    </li>

    {" "}

    <li>
      <code>shadcn</code> – 含有 <code>@shadcn/ui</code> 和 Tailwind CSS 的 React 应用
    </li>

    {" "}
  </ul>

  {" "}

  示例：{" "}

  <pre>
    <code>bun init --react bun init --react=tailwind bun init --react=shadcn</code>
  </pre>

  {" "}
</ParamField>

### 输出 & 文件

<ParamField path="(result)" type="info">
  {" "}

  根据所选项初始化项目文件和配置（例如，创建必要的配置文件和初始目录结构）。具体文件根据模板不同而异。{" "}
</ParamField>

### 全局配置 & 环境

<ParamField path="--cwd" type="string">
  {" "}

  以不同的工作目录运行 <code>bun init</code>（在脚本中很有用）。{" "}
</ParamField>

### 帮助

<ParamField path="--help" type="boolean">
  {" "}

  打印此帮助菜单。别名：<code>-h</code>{" "}
</ParamField>

### 示例

* 接受所有默认值

  ```bash terminal icon="terminal" theme={"theme":{"light":"github-light","dark":"dracula"}}
  bun init -y
  ```

* React

  ```bash terminal icon="terminal" theme={"theme":{"light":"github-light","dark":"dracula"}}
  bun init --react
  ```

* React + Tailwind CSS

  ```bash terminal icon="terminal" theme={"theme":{"light":"github-light","dark":"dracula"}}
  bun init --react=tailwind
  ```

* React + @shadcn/ui
  ```bash terminal icon="terminal" theme={"theme":{"light":"github-light","dark":"dracula"}}
  bun init --react=shadcn
  ```
