rembrembdocs

Skip to main content

Bun home pagelight logodark logo

[Runtime

](../../../index.md)[Package Manager

](../../../pm/cli/install/index.md)[Bundler

](../../../bundler/index.md)[Test Runner

](../../../test/index.md)[Guides

](../../index.md)[Reference

](https://bun.com/reference)[Blog

](https://bun.com/blog)[Feedback

](../../../feedback/index.md)

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.

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.

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.

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

Was this page helpful?

Suggest editsRaise issue

[

Upgrade Bun to the latest version

Previous

](../upgrade/index.md)[

Get the current Bun version

Next

](../version/index.md)