Skip to main content
Bun automatically loads environment variables from .env files, making configuration management seamless across development, testing, and production environments.

Quick Start

Create a .env file in your project root:
.env
Access variables in your code:
Access environment variables
No configuration needed - Bun loads .env automatically!

.env File Priority

Bun loads multiple .env files in priority order:
1

Process Environment

Variables already set in the shell (highest priority)
2

.env.local

Local overrides (gitignored by convention)
3

.env.[environment].local

Environment-specific local overrides:
  • .env.development.local
  • .env.production.local
  • .env.test.local
4

.env.[environment]

Environment-specific configuration:
  • .env.development
  • .env.production
  • .env.test
5

.env

Base configuration (lowest priority)
Environment detection:
  • NODE_ENV=production → loads .env.production
  • NODE_ENV=development → loads .env.development (default)
  • NODE_ENV=test → loads .env.test
Implementation: src/env_loader.zig:1-18 - .env file detection

.env File Format

Standard .env syntax:
.env syntax

Escaping

Escaping special characters

Accessing Variables

process.env

Node.js-compatible API:
process.env

Bun.env

Bun-specific API (read-only):
Bun.env
Differences:
  • Bun.env is read-only
  • process.env allows runtime modifications
  • Both reference the same underlying data

Custom .env Files

Load specific .env files:

CLI Flag

Custom env file

bunfig.toml

bunfig.toml
Implementation: src/bunfig.zig:151-186 - Env config parsing

Programmatic Loading

Manual loading

Environment-Specific Configuration

Development

.env.development

Production

.env.production

Testing

.env.test

Local Overrides

.env.local (gitignored)
Add to .gitignore:
.gitignore

Type Safety

Declare Types

env.d.ts

Validate Variables

Validation

Zod Schema

Zod validation

Special Variables

NODE_ENV

Determines which .env.[environment] file loads:
NODE_ENV

PATH

System executable search paths:
PATH

HOME / USERPROFILE

User home directory:
Home directory

Proxy Variables

HTTP/HTTPS proxy configuration:
Proxy variables
Implementation: src/env_loader.zig:167-198 - HTTP proxy detection

TLS Configuration

TLS variables
Implementation: src/env_loader.zig:144-161 - TLS reject unauthorized

Security Best Practices

Never commit .env files containing secrets to version control!

1. Gitignore .env Files

.gitignore

2. Use .env.example

Commit a template without secrets:
.env.example

3. Validate Required Variables

Startup validation

4. Restrict Permissions

File permissions

5. Use Secrets Management

For production, use proper secrets management:
  • AWS Secrets Manager
  • HashiCorp Vault
  • Azure Key Vault
  • Google Secret Manager
Fetch from secrets manager

Environment Loading Implementation

Bun’s environment variable loading:
Loader structure (src/env_loader.zig:7-29)
Loading order:
  1. Check each environment file
  2. Parse key=value pairs
  3. Expand variables (${VAR})
  4. Merge into environment (lower priority files don’t override)

Advanced Usage

Environment Detection

Environment detection
Implementation: src/env_loader.zig:70-76 - CI detection

Dynamic Configuration

Dynamic config

Variable Expansion

Variable expansion

Debugging

Print environment

Check Variable Loading

Debug env loading

Verify .env Parsing

Test .env file

Troubleshooting

Variables Not Loading

  1. Check file location: .env must be in project root
  2. Check file name: Must be exactly .env (no spaces)
  3. Check syntax: KEY=value (no spaces around =)
  4. Check NODE_ENV: Correct environment file loaded?

Variables Undefined

Check if loaded

Priority Issues

Check which file sets the variable:
Check all .env files