Skip to main content
Bun reads your .env files automatically and provides idiomatic ways to read and write your environment variables programmatically. You can also configure parts of Bun’s runtime behavior 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 the value of NODE_ENV)
  • .env.local
.env
You can also set variables on the command line.
For a cross-platform solution, use Bun Shell, for example through bun exec.
On Windows, package.json scripts called with bun run automatically use the Bun Shell, so the following is also cross-platform.
package.json
Or set them programmatically by assigning a property to process.env.

Manually specifying .env files

The --env-file flag overrides which .env files Bun loads. It works both when running a file with bun and when running package.json scripts.

Disabling automatic .env loading

Use --no-env-file to disable Bun’s automatic .env file loading, for example in production or CI/CD pipelines where you want to rely solely on system environment variables.
You can also disable it in bunfig.toml:
bunfig.toml
Files passed with --env-file still load even when default loading is disabled.

Quotation marks

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

Expansion

Bun automatically expands environment variables, so you can reference previously-defined variables.
.env
This is useful for constructing connection strings or other compound values.
.env
To disable expansion, escape the $ with a backslash.
.env

dotenv

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

Reading environment variables

Read the current environment variables from process.env.
Bun also exposes these variables as Bun.env and import.meta.env, both aliases of process.env.
To print all currently-set environment variables, run bun --print process.env.

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, use interface merging.
Add this declaration to any file in your project. It globally adds the AWESOME property to process.env and Bun.env.

Configuring Bun

Bun reads these environment variables to configure aspects of its behavior.

Runtime transpiler caching

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

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 4 KB.
  • The sourcemap for the transpiled output of the file
Cached files use the .pile extension.