One .env file stops being enough once development and production builds need different values, or once a teammate needs a setting that only applies to their own machine. Expo CLI reads up to four standard .env files for each command, chosen by the command's mode, and when two files set the same variable the one with higher priority wins. Working out that winner tells you which value the app gets before you run anything.
Key terms
- .env.development: a file that loads only when a command runs in development mode, with
.env.productionas its production counterpart. - .env.local: a file for configuration specific to your machine, loaded in both modes.
- NODE_ENV: the standard Node.js variable that names the mode code runs in, typically
development,productionortest.
Priority by mode
Each Expo command runs in development or production mode, and the mode picks the files. Files listed first take priority:
| Mode | Files, highest priority first |
|---|---|
| Development | .env.development.local, .env.local, .env.development, .env |
| Production | .env.production.local, .env.local, .env.production, .env |
Priority applies variable by variable. A variable set in only one of the loaded files comes from that file, and a variable set in several comes from the one listed first.
The two rows share .env.local and .env. A value in .env.local beats the mode files in both modes, and .env is the fallback for both.
Which mode each command runs in
The command decides the mode, through its own flags:
| Command | Mode |
|---|---|
npx expo start | Development. Production with --no-dev. |
npx expo export | Production. Development with --dev. |
npx expo config | Development. Production when it runs on Expo Build, Expo's hosted service for building app binaries. |
Since SDK 58, each command sets NODE_ENV to its mode and replaces any value you set. NODE_ENV=production npx expo start runs in development mode and loads .env.development. To load the production files from the dev server, pass --no-dev instead.
The reason is that many tools read NODE_ENV in their own ways. Running NODE_ENV=production npm install, for example, skips your devDependencies. Expo stopped overloading the same variable to switch between .env files.
Working out a value
The four files in the .env example set a log level, a feedback address and the address of a server on your local network. Walk down the list for the mode, variable by variable:
npx expo startruns in development.EXPO_PUBLIC_LOG_LEVELisdebugfrom.env.development,EXPO_PUBLIC_FEEDBACK_EMAILcomes from.env, andEXPO_PUBLIC_API_HOSTcomes from.env.local.npx expo start --no-devruns in production. The log level becomeserrorfrom.env.production. The other two keep their values, since.env.localand.envload in both modes.NODE_ENV=production npx expo startgives the same values as the first run, because the command replacesNODE_ENVwith development.
Now add EXPO_PUBLIC_LOG_LEVEL=info to .env.local. Both npx expo start and npx expo start --no-dev give info, because .env.local ranks above .env.development and .env.production.
What goes into git
You may commit .env and the other standard files. .env.local files generally belong in .gitignore, because they hold configuration for your own machine, such as your computer's network IP address when the app makes requests to a server running on it. The default project template's .gitignore already has the rule, under a comment:
# local env files
.env*.local
That split fits the priority table. The committed .env, .env.development and .env.production hold the values the whole team shares, and anyone who needs a different value on their own machine sets it in .env.local, which beats the committed files in both modes without changing them.
Files Expo ignores
Since SDK 58, no Expo command loads any other .env file, such as .env.staging or .env.test. If a project still has such a file, move its contents into one of the standard files from the table.
To switch between staging and production values on your machine without Expo's hosted services, write a script that overwrites .env.local or .env with the contents for the environment you want to work with.
Code Examples
# .env
EXPO_PUBLIC_LOG_LEVEL=warn
EXPO_PUBLIC_FEEDBACK_EMAIL=feedback@trailmix.dev
# .env.development
EXPO_PUBLIC_LOG_LEVEL=debug
# .env.production
EXPO_PUBLIC_LOG_LEVEL=error
# .env.local (ignored by git)
EXPO_PUBLIC_API_HOST=192.168.1.20