Skip to main content
Bun reads your .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:
  1. .env
  2. .env.production, .env.development, or .env.test (based on the value of NODE_ENV)
  3. .env.local
Variables defined in files with higher precedence override those in files with lower precedence.
.env

Command line

For a cross-platform solution, use Bun Shell. The bun exec command runs a shell command cross-platform:
On Windows, package.json scripts called with bun run automatically use the Bun shell, making inline env vars cross-platform:
package.json

Programmatically

Assign directly to process.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.
You can also disable it in 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 via process.env, Bun.env, or import.meta.env. All three are equivalent.
To print all currently-set environment variables for debugging:

.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
This is useful for constructing connection strings:
.env
To disable expansion for a specific variable, escape the $ with a backslash:
.env

TypeScript

All process.env properties are typed as string | undefined by default:
To add autocompletion and tell TypeScript to treat a variable as a non-optional string, use interface merging. Add the following declaration to any file in your project:
Now 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:

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.
To disable the cache: