.env files automatically and provides idiomatic ways to read and write environment variables programmatically. You no longer need packages like dotenv or dotenv-expand.
Setting environment variables
.env files
Bun reads the following files automatically, listed in order of increasing precedence:.env.env.production,.env.development, or.env.test(based on the value ofNODE_ENV).env.local
.env
Command line
Programmatically
Assign directly toprocess.env at runtime:
Specifying .env files
Use--env-file to load a specific .env file instead of the defaults. You can pass multiple --env-file flags; later files take higher precedence.
Disabling automatic .env loading
Use--no-env-file to skip automatic .env loading entirely. This is useful in production or CI environments where environment variables are injected by the platform.
bunfig.toml:
bunfig.toml
Explicitly provided
--env-file flags still load their files even when --no-env-file or env = false is set.Reading environment variables
Access the current environment viaprocess.env, Bun.env, or import.meta.env. All three are equivalent.
.env file syntax
Quotation marks
Bun supports double quotes, single quotes, and backtick template literals:.env
Variable expansion
Environment variables are automatically expanded. You can reference previously-defined variables in later declarations:.env
.env
$ with a backslash:
.env
TypeScript
Allprocess.env properties are typed as string | undefined by default:
process.env.MY_REQUIRED_VAR and Bun.env.MY_REQUIRED_VAR are typed as string (not string | undefined).
Bun-specific environment variables
These environment variables configure aspects of Bun’s own behavior:| Variable | Description |
|---|---|
NODE_TLS_REJECT_UNAUTHORIZED | Set to 0 to disable SSL certificate validation. Use for testing only. |
BUN_CONFIG_VERBOSE_FETCH | Set to curl to log fetch request/response headers. Set to 1 for the same output without curl formatting. |
BUN_RUNTIME_TRANSPILER_CACHE_PATH | Directory for the runtime transpiler cache. Set to 0 or empty string to disable caching. |
TMPDIR | Directory for temporary files during bundling. Defaults to /tmp (Linux) or /private/tmp (macOS). |
NO_COLOR | Set to 1 to disable ANSI color output. |
FORCE_COLOR | Set to 1 to force ANSI color output even when NO_COLOR is set. |
BUN_CONFIG_MAX_HTTP_REQUESTS | Maximum concurrent HTTP requests for fetch and bun install. Defaults to 256. |
BUN_CONFIG_NO_CLEAR_TERMINAL_ON_RELOAD | Set to true to prevent bun --watch from clearing the terminal on reload. |
DO_NOT_TRACK | Set to 1 to disable crash report uploads and telemetry. |
BUN_OPTIONS | Prepends CLI arguments to every Bun invocation. For example, BUN_OPTIONS="--hot" makes bun run dev behave like bun --hot run dev. |
Runtime transpiler cache
For files larger than 50 KB, Bun caches transpiled output to speed up subsequent runs. The cache is global, content-addressable, and safe to delete at any time.It is recommended to disable the transpiler cache when using ephemeral filesystems like Docker. Bun’s official Docker images disable this cache automatically.