> ## Documentation Index
> Fetch the complete documentation index at: https://bun.manicule.dev/llms.txt
> Use this file to discover all available pages before exploring further.
> For every page in one file, fetch https://bun.manicule.dev/llms-full.txt (large).
> To search and read these docs as tools, connect the MCP server at https://bun.manicule.dev/api/mcp.
> Every agent surface: https://bun.manicule.dev/.well-known/llms-manifest.json

# Detect when code is executed with Bun (/guides/util/detect-bun)

<!-- agent-signals: reading_time_min: 1 · est_tokens: 147 · updated: 2026-08-02 -->
Related: [Upgrade Bun to the latest version](/guides/util/upgrade.md), [Get the current Bun version](/guides/util/version.md), [Hash a password](/guides/util/hash-a-password.md), [Generate a UUID](/guides/util/javascript-uuid.md), [Encode and decode base64 data](/guides/util/base64.md), [Compress and decompress data with gzip](/guides/util/gzip.md)

Check `process.versions.bun` to detect whether code is running in Bun. This works in both JavaScript and TypeScript without extra type definitions.

```ts
if (process.versions.bun) {
  // this code will only run when the file is run with Bun
}
```

***

Alternatively, check for the `Bun` global, the same way you'd check for `window` to detect a browser.

<Note>In TypeScript, this is a type error unless `@types/bun` is installed. Install it with `bun add -d @types/bun`.</Note>

```ts
if (typeof Bun !== "undefined") {
  // this code will only run when the file is run with Bun
}
```
