Everything you name with EXPO_PUBLIC_ ends up readable in the app you ship, and values you copy into the app config can reach the app as well. That is fine for a public API address and a real problem for a payment provider's secret key. The prefix only controls what Expo inlines into your code, so a variable without it can still leak through the config.

Key terms

  • Client bundle: the JavaScript bundle that Expo's Metro config builds for your app, where EXPO_PUBLIC_ values get inlined.
  • EXPO_NO_DOTENV: an environment variable that, set to 1, stops Expo CLI from loading .env files.
  • EXPO_NO_CLIENT_ENV_VARS: an environment variable that, set to 1, stops Expo's Metro config from inlining EXPO_PUBLIC_ variables in client bundles.

Anything prefixed is public

Never store private keys or other sensitive secrets in EXPO_PUBLIC_ variables. Their values are visible in plain text in the compiled app, and a user who runs your app has access to all of its code and every environment variable embedded in it.

A public API address or a feature label is fine behind the prefix. A key that can charge cards or write to your database is not. A value the user must never see can't live in the app at all, so the code that uses it has to run somewhere you control, such as your own server.

Removing the prefix keeps a variable out of your components. It does not keep it out of the app config.

What app.config.ts sees

Expo CLI loads your .env files into its own process, and npx expo config loads them in its mode before it evaluates and prints the app config. So process.env inside app.config.ts sees every variable from the loaded files, with or without the prefix. You can also set a variable for a single command by putting it in front, as in APP_ENV=staging npx expo config.

Together with a dynamic config, that lets one project switch its configuration between development, staging and production. The app.config.ts example picks the app's name from APP_ENV and copies the environment into extra:

ts
environment: process.env.APP_ENV ?? 'production',

Run APP_ENV=staging npx expo config --type public, and both the staging name and extra.environment show up in the public config.

Where config values end up

extra stays in the public config that builds embed, as the Reading Config at Runtime lesson covers. So if app.config.ts copies process.env.PAYMENTS_SECRET into extra, every copy of the app can read it through Constants.expoConfig.extra, even though the variable has no prefix.

Older setups did this on purpose. With direnv, a dynamic config read process.env and set the values on extra so the JavaScript could read them through expo-constants. For values your JavaScript needs, move those references into your code with the EXPO_PUBLIC_ prefix instead. The prefix doesn't make the value any more private than extra did. Both end up readable in the shipped app, so either place is only for values a user may see.

Turning the features off

Environment variable support in Expo CLI has two parts, and you can disable each one:

  • EXPO_NO_DOTENV=1, set before any Expo CLI command, stops Expo CLI from loading .env files into the process, for every Expo CLI command.
  • EXPO_NO_CLIENT_ENV_VARS=1 disables the inline serialization of environment variables in the client bundle that Expo's Metro config performs. The flag is false by default.

When environment variables misbehave, try disabling one or both, for example EXPO_NO_DOTENV=1 npx expo start, and see whether the problem follows.

Code Examples

typescript
import { ExpoConfig, ConfigContext } from 'expo/config';

const appEnv = process.env.APP_ENV;

export default ({ config }: ConfigContext): ExpoConfig => ({
  ...config,
  name: appEnv === 'staging' ? 'Trailmix Staging' : 'Trailmix',
  slug: 'trailmix',
  extra: {
    ...config.extra,
    environment: process.env.APP_ENV ?? 'production',
  },
});
✓ Completed