日本語

Bun .env file load order and which value wins

​​Bun​environment variables,TypeScript

Bun loads .env files without any configuration. Besides .env, it also reads .env.development, .env.local, and others. If the same variable name appears in more than one file, the value from the file read later wins, in a fixed order.

If you want different API keys or database URLs for development and production, or if a value you wrote in .env does not show up in the environment, knowing this order and its exceptions helps you narrow down the cause. I wrote the same variable in every file and checked which value remained.

Note

The test environment was Ubuntu 24.04 (x86_64) with Bun 1.4.4. I ran the Node.js script with Node.js 22. To keep shell environment variables out of the results, I ran the commands with env -i, which leaves almost no environment variables set. I did not test Windows or macOS, loading through bun install, or running Node.js with bun --bun. I checked the official documentation in the source files of the version I tested (tag bun-v1.4.4) on GitHub, and the links point there.

The load order has four stages, and later files win

The official environment variables page lists the files Bun loads automatically, from lowest to highest priority, as follows:

  1. .env
  2. .env.production, .env.development, or .env.test (one of them, depending on the value of NODE_ENV)
  3. .env.local (not loaded when NODE_ENV=test)
  4. .env.production.local, .env.development.local, or .env.test.local (one of them, depending on the value of NODE_ENV)

To check this, I wrote WHO=<file name> in all eight files. I also wrote a separate variable in each stage (IN_ENV, IN_MODE, IN_LOCAL, and IN_MODE_LOCAL). For example, the contents of .env.development are:

text
WHO=.env.development
IN_MODE=development

I created show.ts, which only prints the values.

ts
const keys = ["NODE_ENV", "WHO", "IN_ENV", "IN_MODE", "IN_LOCAL", "IN_MODE_LOCAL"];
for (const k of keys) console.log(`${k}=${process.env[k] ?? "(なし)"}`);

When I ran it without NODE_ENV, WHO kept the value from .env.development.local. The other variables show that all four files were read.

text
$ bun show.ts
NODE_ENV=(なし)
WHO=.env.development.local
IN_ENV=yes
IN_MODE=development
IN_LOCAL=yes
IN_MODE_LOCAL=development

I deleted the files one at a time, in the order .env.development.local, .env.local, then .env.development. The value of WHO changed to .env.local, then .env.development, then .env, which matches the documented order.

Diagram of the order in which Bun reads .env files. When run without NODE_ENV, it reads .env, .env.development, .env.local, and .env.development.local in that order, and the value from .env.development.local remains. Values passed from the shell take priority over all .env files.
The order in which .env files are read when NODE_ENV is not set

NODE_ENV changes which files are read, and without it, development files are read

I ran the same show.ts with different NODE_ENV values. The results are below.

How you run it WHO value .env.local
bun show.ts .env.development.local Read
NODE_ENV=production bun show.ts .env.production.local Read
NODE_ENV=test bun show.ts .env.test.local Not read
NODE_ENV=staging bun show.ts .env.development.local Read

When NODE_ENV was not set, the development files were read. In this case process.env.NODE_ENV stays unset and does not get the value development. When I used staging, which has no matching files, Bun also read the development files.

If you put production values in .env.production, they are not read unless you start Bun with NODE_ENV=production.

A NODE_ENV written in .env does not change which files are read

I added NODE_ENV=production at the end of .env and ran the script without setting NODE_ENV.

text
$ bun show.ts
NODE_ENV=production
WHO=.env.development.local
IN_ENV=yes
IN_MODE=development
IN_LOCAL=yes
IN_MODE_LOCAL=development

process.env.NODE_ENV became production, but the files that were read were still the development ones. Which files Bun reads is decided by the NODE_ENV value present when Bun starts, not by the one in .env.

Values passed from the shell take priority over .env

When I put WHO=shell in front of the command, the value shell was used instead of the one from .env.development.local. When I passed an empty value, such as WHO=, the value stayed empty and was not filled in from .env.

text
$ WHO=shell bun show.ts
NODE_ENV=(なし)
WHO=shell
IN_ENV=yes
IN_MODE=development
IN_LOCAL=yes
IN_MODE_LOCAL=development
$ WHO= bun show.ts
NODE_ENV=(なし)
WHO=
IN_ENV=yes
IN_MODE=development
IN_LOCAL=yes
IN_MODE_LOCAL=development

If your CI or server sets environment variables, a variable with the same name in .env does not overwrite them. In the other direction, if an old value remains in your shell, changing .env may seem to have no effect.

bun test reads .env.test and does not read .env.local

When I printed the same variables from inside bun test, NODE_ENV was test, and the files read were .env.test and .env.test.local. The value from .env.local (IN_LOCAL) was not present.

text
$ bun test env.test.ts
bun test v1.4.4 (38afee6c8)

env.test.ts:
NODE_ENV=test
WHO=.env.test.local
IN_MODE=test
IN_LOCAL=(なし)
IN_MODE_LOCAL=test

If you keep local development values in .env.local, they do not leak into tests. Values used only by tests belong in .env.test.

The page that describes the runtime behavior of tests says that if NODE_ENV is set in an environment variable or a .env file, bun test does not change it to test. However, when I added NODE_ENV=development to .env and ran bun test, the result was the same as above, and NODE_ENV stayed test. When I put NODE_ENV=development or NODE_ENV=production in front of the bun test command, process.env.NODE_ENV took that value. However, in both cases the files read were still .env.test and .env.test.local.

text
$ NODE_ENV=development bun test env.test.ts
bun test v1.4.4 (38afee6c8)

env.test.ts:
NODE_ENV=development
WHO=.env.test.local
IN_MODE=test
IN_LOCAL=(なし)
IN_MODE_LOCAL=test

Bun reads the .env in the folder where you run the command

Bun looked for .env in the folder where you run bun, not in the folder that contains the script. I created a folder named sub in the project, put a .env containing only WHO=sub/.env and a copy of show.ts there, and tested it.

Folder where you run it Command WHO value
Project folder bun sub/show.ts .env.development.local
sub bun show.ts sub/.env
sub bun ../show.ts sub/.env
Project folder bun --cwd sub show.ts sub/.env

When I ran it from sub, the .env.local and other files in the parent folder were not read, and variables such as IN_ENV were reported as unset. Bun does not search parent folders. When you run the command from a different folder, use --cwd to specify the project folder.

The values from .env were not passed to package.json scripts

I added the following three scripts to package.json and ran them with bun run.

json
{
  "name": "env-demo",
  "private": true,
  "scripts": {
    "show": "bun show.ts",
    "print": "printenv WHO || echo 'WHO はありません'",
    "node-print": "node print-env.mjs"
  }
}

print-env.mjs is a Node.js script that only prints process.env.WHO.

js
console.log(`WHO=${process.env.WHO}`);

bun run show printed the same value as before, because bun show.ts, started inside the script, reads .env itself. However, bun run print, which prints the environment variable with a shell command, and bun run node-print, which runs Node.js, did not receive the values from .env. When I used --env-file, the values from the specified file were passed.

text
$ bun run print
$ printenv WHO || echo 'WHO はありません'
WHO はありません
$ bun --env-file=.env.local run print
$ printenv WHO || echo 'WHO はありません'
.env.local
$ bun run node-print
$ node print-env.mjs
WHO=undefined
$ bun --env-file=.env.local run node-print
$ node print-env.mjs
WHO=.env.local

The official description of --env-file also says that this option can be used when running package.json scripts. If you want to pass .env values to commands other than Bun (shell commands or Node.js scripts), specify them explicitly with --env-file.

With –env-file, the fixed files are not read

When you use --env-file, Bun does not read the four stages above. It reads only the files you specify. When you specify more than one, the value from the file written later remains. Values passed from the shell still take priority in this case.

show2.ts is a file that prints WHO, IN_ENV, IN_MODE, IN_LOCAL, and IN_CUSTOM, like show.ts. I wrote WHO=custom.env and IN_CUSTOM=yes in custom.env, and only WHO=second.env in second.env.

text
$ bun --env-file=custom.env show2.ts
WHO=custom.env
IN_ENV=(なし)
IN_MODE=(なし)
IN_LOCAL=(なし)
IN_CUSTOM=yes
$ bun --env-file=custom.env --env-file=second.env show2.ts
WHO=second.env
IN_ENV=(なし)
IN_MODE=(なし)
IN_LOCAL=(なし)
IN_CUSTOM=yes
Warning

Specifying a file that does not exist with --env-file=nothing.env did not produce an error, and the exit code was 0. In this case, no value from any .env file was loaded. If you mistype a file name, the program can run without its settings and you may not notice.

To turn off all automatic loading, use --no-env-file or write env = false in bunfig.toml. In both cases, none of the values from .env were loaded. When I used --no-env-file together with --env-file=custom.env, only custom.env was read (explanation of how to disable automatic loading).

A $ variable written in a .env value is replaced with another value

If you write $HOST in a .env value, it is replaced with the value of another variable (official explanation of variable expansion). This is useful when you combine several values, such as a database URL. I tested it with the following .env. I wrote LOCAL_ONLY in .env.local.

text
HOST=localhost
PORT=5432
DB_URL=postgres://$HOST:$PORT/app
BRACE=${HOST}_db
ESCAPED=price\$HOST
SINGLE='$HOST'
DOUBLE="$HOST"
LATER=$DEFINED_LATER
DEFINED_LATER=yes
FROM_LOCAL=$LOCAL_ONLY
PASSWORD=pa$sword
PASSWORD_ESCAPED=pa\$sword

The results are below. Values are printed with JSON.stringify.

text
$ bun expand.ts
DB_URL="postgres://localhost:5432/app"
BRACE="localhost_db"
ESCAPED="price$HOST"
SINGLE="localhost"
DOUBLE="localhost"
LATER="yes"
FROM_LOCAL="from-local"
PASSWORD="pa"
PASSWORD_ESCAPED="pa$sword"

Only the part written as \$ kept $HOST as literal text. Values in single quotes were also expanded. The official documentation says you can refer to variables defined earlier, but I also confirmed that a variable defined on a later line (LATER) and one defined in another file (FROM_LOCAL) can be referenced.

When I ran it with HOST=example.com in the shell, DB_URL became postgres://example.com:5432/app. The value passed from the shell is also used for expansion.

When you write a value that contains $ as-is, like PASSWORD, $sword is treated as an undefined variable and replaced with an empty string, so the value becomes only pa. If a password or token contains $, write it with \$, as in PASSWORD_ESCAPED.

The values above can also be read through Bun.env and import.meta.env, in addition to process.env. In the test environment, all three were the same object (Bun.env === process.env was true).

​Posted by 86