Skip to main content
Pass --define to bun build or bun build --compile to inject build-time constants into your application. Use it to embed metadata like build versions, timestamps, or configuration flags directly into your compiled executables.
terminal

Why use build-time constants?

Build-time constants are embedded directly into your compiled code, making them:
  • Zero runtime overhead - No environment variable lookups or file reads
  • Immutable - Values are baked into the binary at compile time
  • Optimizable - Dead code elimination can remove unused branches
  • Secure - No external dependencies or configuration files to manage
This is similar to gcc -D or #define in C/C++, but for JavaScript/TypeScript.

Basic usage

With bun build

terminal

With bun build --compile

terminal

JavaScript API

build.ts

Common use cases

Version information

Embed version and build metadata directly into your executable:

Feature flags

Use build-time constants to enable/disable features:
src/version.ts

Configuration

Replace configuration objects at build time:
src/version.ts

Advanced patterns

Environment-specific builds

Create different executables for different environments:

Using shell commands for dynamic values

Generate build-time constants from shell commands:

Build automation script

Create a build script that injects build metadata:

Important considerations

Value format

Values must be valid JSON. Bun parses each value and inlines it as a JavaScript expression:

Property keys

Keys can be property access patterns, not just simple identifiers:
Use this to inline environment variables at build time:

TypeScript declarations

For TypeScript projects, declare your constants to avoid type errors:

Cross-platform compatibility

When building for multiple platforms, constants work the same way: