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

# 检测代码是否在 Bun 中执行

检查 `process.versions.bun` 以检测代码是否在 Bun 中运行。这在 JavaScript 和 TypeScript 中均有效，无需额外的类型定义。

```ts theme={null}
if (process.versions.bun) {
  // 此代码仅在文件使用 Bun 运行时执行
}
```

***

或者，检查 `Bun` 全局变量是否存在，就像检查 `window` 以检测浏览器一样。

<Note>
  在 TypeScript 中，除非安装了 `@types/bun`，否则这会是类型错误。使用 `bun add -d @types/bun` 安装。
</Note>

```ts theme={null}
if (typeof Bun !== "undefined") {
  // 此代码仅在文件使用 Bun 运行时执行
}
```
