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.production as 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, production or test.

Priority by mode

Each Expo command runs in development or production mode, and the mode picks the files. Files listed first take priority:

ModeFiles, 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:

CommandMode
npx expo startDevelopment. Production with --no-dev.
npx expo exportProduction. Development with --dev.
npx expo configDevelopment. 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 start runs in development. EXPO_PUBLIC_LOG_LEVEL is debug from .env.development, EXPO_PUBLIC_FEEDBACK_EMAIL comes from .env, and EXPO_PUBLIC_API_HOST comes from .env.local.
  • npx expo start --no-dev runs in production. The log level becomes error from .env.production. The other two keep their values, since .env.local and .env load in both modes.
  • NODE_ENV=production npx expo start gives the same values as the first run, because the command replaces NODE_ENV with 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

bash
# .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
✓ Completed