Values like the address of your API server or a label for the current environment change from one machine or build to the next, so they belong outside your source code. Expo CLI reads them from .env files whenever you run one of its commands, such as npx expo start, and automatically hands the ones named with the EXPO_PUBLIC_ prefix to your JavaScript.

Key terms

  • .env file: a text file in the project root that sets one variable per line in the form NAME=VALUE.
  • EXPO_PUBLIC_: the name prefix that makes Expo CLI pass a variable from .env to your app's JavaScript.
  • Inlining: Expo CLI replacing a process.env.EXPO_PUBLIC_ reference in your code with the value set in .env.
  • Full reload: reloading the whole app, for example by shaking the device and tapping Reload in Expo Go or your development build.

The prefix and the dot

Create .env in the root of the project and put one variable per line in the form EXPO_PUBLIC_[NAME]=VALUE. Expo CLI loads the file and replaces each reference to process.env.EXPO_PUBLIC_[NAME] in your code with the matching value. Two conditions decide whether a given line qualifies.

The name has to start with EXPO_PUBLIC_. A variable such as BUILD_CHANNEL can sit in the same file, but process.env.BUILD_CHANNEL in a component is not replaced.

The reference has to be static dot notation, the text process.env. followed by the full name. Destructuring the variable out of process.env or building its name at runtime doesn't count, so neither of the last two lines here is inlined:

tsx
const label = process.env.EXPO_PUBLIC_ENV_LABEL; // inlined

const { EXPO_PUBLIC_ENV_LABEL } = process.env; // not inlined
const value = process.env[`EXPO_PUBLIC_${key}`]; // not inlined

Only your own project's files get the replacement. Code inside node_modules is left as it is, for security.

Why destructuring can fool you in development

A line that breaks the dot rule can still seem to work while you develop. Development bundles set every EXPO_PUBLIC_ variable as a property of process.env so the values persist between Fast Refresh updates, and a destructured read may find its value there. Only prefixed variables are set this way, so a variable like BUILD_CHANNEL never reaches the app through that route. Production bundles add none of that and expect every variable to have been inlined. Write each read as process.env.EXPO_PUBLIC_NAME, even when another form looks fine on your device.

Picking up an edited value

After you edit .env, there is no need to restart Expo CLI or clear the cache. Once the dev server is up, npx expo start watches the .env files (unless the project is web only) and reloads them into its own environment when one of them changes.

The running app does not show the new value on its own until you do a full reload.

A staging banner that reads two variables

The .env example sets two prefixed variables for a staging setup, plus BUILD_CHANNEL without the prefix. The getStagingBanner function builds the text for a staging banner and reads the two prefixed ones inside its body:

ts
const label = process.env.EXPO_PUBLIC_ENV_LABEL;

Both reads use plain dot notation and the full prefixed name, so Expo CLI replaces them with Staging and the support address from .env. The function leaves BUILD_CHANNEL alone, because a reference to it would not be replaced.

To see a change arrive, keep npx expo start running and edit the label line in .env to EXPO_PUBLIC_ENV_LABEL=Staging EU. Nothing changes on the device until you do a full reload. After the reload, getStagingBanner() returns text that starts with Staging EU, and you never restarted the dev server.

Code Examples

bash
EXPO_PUBLIC_ENV_LABEL=Staging
EXPO_PUBLIC_SUPPORT_EMAIL=help@trailmix.dev
BUILD_CHANNEL=internal
typescript
export function getStagingBanner(): string {
  const label = process.env.EXPO_PUBLIC_ENV_LABEL;
  const supportEmail = process.env.EXPO_PUBLIC_SUPPORT_EMAIL;

  return `${label} build. Questions? Write to ${supportEmail}`;
}
✓ Completed