Skip to main content
Bun reads your .env files automatically and provides idiomatic ways to read and write your environment variables programmatically. Plus, some aspects of Bun’s runtime behavior can be configured with Bun-specific environment variables.

Setting environment variables

Bun reads the following files automatically (listed in order of increasing precedence).
  • .env
  • .env.production, .env.development, .env.test (depending on value of NODE_ENV)
  • .env.local
.env
Variables can also be set via the command line.
For a cross-platform solution, you can use bun shell. For example, the bun exec command.
On Windows, package.json scripts called with bun run will automatically use the bun shell, making the following also cross-platform.
package.json
Or programmatically by assigning a property to process.env.

Manually specifying .env files

Bun supports --env-file to override which specific .env file to load. You can use --env-file when running scripts in bun’s runtime, or when running package.json scripts.

Disabling automatic .env loading

Use --no-env-file to disable Bun’s automatic .env file loading. This is useful in production environments or CI/CD pipelines where you want to rely solely on system environment variables.
This can also be configured in bunfig.toml:
bunfig.toml
Explicitly provided environment files via --env-file will still be loaded even when default loading is disabled.

Quotation marks

Bun supports double quotes, single quotes, and template literal backticks:
.env

Expansion

Environment variables are automatically expanded. This means you can reference previously-defined variables in your environment variables.
.env
This is useful for constructing connection strings or other compound values.
.env
This can be disabled by escaping the $ with a backslash.
.env

dotenv

Bun reads .env files automatically, so dotenv and dotenv-expand are unnecessary.

Reading environment variables

The current environment variables can be accessed via process.env.
Bun also exposes these variables via Bun.env and import.meta.env, which are aliases of process.env.
To print all currently-set environment variables to the command line, run bun --print process.env. This is useful for debugging.

TypeScript

In TypeScript, all properties of process.env are typed as string | undefined.
To get autocompletion and tell TypeScript to treat a variable as a non-optional string, we’ll use interface merging.
Add this line to any file in your project. It will globally add the AWESOME property to process.env and Bun.env.

Configuring Bun

These environment variables are read by Bun and configure aspects of its behavior.

Runtime transpiler caching

For files larger than 50 KB, Bun caches transpiled output into $BUN_RUNTIME_TRANSPILER_CACHE_PATH or the platform-specific cache directory. This makes CLIs using Bun load faster. This transpiler cache is global and shared across all projects. It is safe to delete the cache at any time. It is a content-addressable cache, so it will never contain duplicate entries. It is also safe to delete the cache while a Bun process is running. It is recommended to disable this cache when using ephemeral filesystems like Docker. Bun’s Docker images automatically disable this cache.

Disable the runtime transpiler cache

To disable the runtime transpiler cache, set BUN_RUNTIME_TRANSPILER_CACHE_PATH to an empty string or the string "0".

What does it cache?

It caches:
  • The transpiled output of source files larger than 50 KB.
  • The sourcemap for the transpiled output of the file
The file extension .pile is used for these cached files.