Files
bun.sh/docs/guides/util/detect-bun.mdx
Lydia Hallie dce7a02f4d Docs: Minor fixes and improvements (#25284)
This PR addresses several issues opened for the docs:

- Add callout for SQLite caching behavior between prepare() and query()
- Fix SQLite types and fix deprecated exec to run
- Fix Secrets API example
- Update SolidStart guide
- Add bun upgrade guide
- Prefer `process.versions.bun` over `typeof Bun` for detection
- Document complete `bunx` flags
- Improve Nitro preset documentation for Nuxt

Fixes #23165, #24424, #24294, #25175, #18433, #16804, #22967, #22527,
#10560, #14744
2025-12-01 13:32:08 -08:00

29 lines
860 B
Plaintext

---
title: Detect when code is executed with Bun
sidebarTitle: Detect Bun
mode: center
---
The recommended way to detect when code is being executed with Bun is to check `process.versions.bun`. This works in both JavaScript and TypeScript without requiring any additional type definitions.
```ts
if (process.versions.bun) {
// this code will only run when the file is run with Bun
}
```
---
Alternatively, you can check for the existence of the `Bun` global. This is similar to how you'd check for the existence of the `window` variable to detect when code is being executed in a browser.
<Note>
This approach will result in a type error in TypeScript unless `@types/bun` is installed. You can 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
}
```