Edit this page
Learn how to use environment variables in an Expo project.
Environment variables are key-value pairs configured outside your source code that allow your app to behave differently depending on the environment. For example, you can enable or disable certain features when building a test version of your app, or switch to a different API endpoint when building for production.
The Expo CLI will automatically load environment variables with an EXPO_PUBLIC_
prefix from .env files for use within your JavaScript code whenever you use the Expo CLI, such as when running npx expo start
to start your app in local development mode.
Create a .env file in the root of your project directory and add environment-specific variables on new lines in the form of EXPO_PUBLIC_[NAME]=VALUE
:
EXPO_PUBLIC_API_URL=https://staging.example.com
EXPO_PUBLIC_API_KEY=abc123
Now you can use environment variables directly in your source code:
import { Button } from 'react-native';
function Post() {
const apiUrl = process.env.EXPO_PUBLIC_API_URL;
async function onPress() {
await fetch(apiUrl, { ... })
}
return <Button onPress={onPress} title="Post" />;
}
When you run npx expo start
, process.env.EXPO_PUBLIC_API_URL
will be replaced with https://staging.example.com
in your app bundle. Variables can be updated as you edit your code without restarting the Expo CLI or clearing the cache. You will need to perform a full reload (for example, shake gesture and then Reload in Expo Go or your development build) to see the updated value.
Do not store sensitive info, such as private keys, inEXPO_PUBLIC_
variables. These variables will be visible in plain-text in your compiled application.
Expo CLI loads .env files according to the standard .env file resolution and then replaces all references in your code to process.env.EXPO_PUBLIC_[VARNAME]
with the corresponding value set in the .env files. Code inside node_modules is not affected for security purposes.
Every environment variable must be statically referenced as a property of
process.env
using JavaScript's dot notation for it to be inlined. For example, the expression
process.env.EXPO_PUBLIC_KEY
is valid and will be inlined.
Alternative versions of the expression are not supported. For example,
process.env['EXPO_PUBLIC_KEY']
or const {EXPO_PUBLIC_X} = process.env
is invalid and will not
be inlined.
You can define any of the standard .env files, so it is possible to have separate .env, .env.local, .env.production and so on, files and they will load according to the standard priority.
You may choose to commit the default .env file or other standard configurations to version control, but generally .env.local files should be added to your .gitignore:
.env*.local
If you create an environment-specific file, like .env.test, you can load it by setting NODE_ENV
when running the Expo CLI:
-
NODE_ENV=test npx expo start
Environment variables in Expo CLI have two parts and both can be disabled:
EXPO_NO_DOTENV
to 1
before running any Expo CLI command: EXPO_NO_DOTENV=1
.EXPO_NO_CLIENT_ENV_VARS=1
.If you're experiencing issues with environment variables, you can try disabling one or both of these features.
EAS Build uses Metro Bundler to build the JavaScript bundle embedded within your app binary, so it will use .env files uploaded with your build job to inline EXPO_PUBLIC_
variables into your code. EAS Build also lets you define environment variables within build profiles in eas.json and via EAS Secrets. Check out the EAS Build documentation on environment variables and build secrets for more information.
EAS Update uses Metro Bundler in your local environment or CI to build your app bundle, so it will use available .env files to inline EXPO_PUBLIC_
variables into your code. Check out the EAS Update documentation on environment variables for more information.
Update your .env files to prefix any variables used within your JavaScript code with EXPO_PUBLIC_
:
- API_URL=https://myapi.com
+ EXPO_PUBLIC_API_URL=https://myapi.com
If you have any non-standard .env files (for example, .env.staging), you will need to migrate those to one of the standard .env files.
Then update your code to use process.env.EXPO_PUBLIC_[VARNAME]
:
- import Config from "react-native-config";
- const apiUrl = Config.API_URL;
+ const apiUrl = process.env.EXPO_PUBLIC_API_URL;
Using a Babel plugin to transform your environment variable references in your code is similar to how Expo environment variables work. Set your variables inside a .env file and update your variable names to use the EXPO_PUBLIC_
prefix:
- const apiUrl = process.env.API_URL;
+ const apiUrl = process.env.EXPO_PUBLIC_API_URL;
Then you can remove the plugin from your Babel config:
module.exports = function (api) {
api.cache(true);
return {
presets: ['babel-preset-expo'],
-- plugins: ['transform-inline-environment-variables'],
};
};
After updating your Babel config file, be sure to clear your cache with npx expo start -c
.
Move any environment variables used in your JavaScript from their .envrc file to a .env file and prefix it with EXPO_PUBLIC_
.
Previously with direnv
, you would need to use a dynamic app config that reads from process.env
to set environment variables on the extra
field so they can be used in your JavaScript code via expo-constants
. Move those references directly into your code, adding the EXPO_PUBLIC_
prefix:
- import Constants from 'expo-constants';
- const apiUrl = Constants.expoConfig.extra.apiUrl;
+ const apiUrl = process.env.EXPO_PUBLIC_API_URL;
direnv
automatically loads and unloads environment variables in your shell depending on your current directory, meaning it can affect the environment for any process running in that directly, not just the Expo CLI. You will likely want to continue usingdirenv
for other environment variables that are not used in your JavaScript code.
Never store sensitive secrets in environment variables that are prefixed with EXPO_PUBLIC_
. When an end-user runs your app, they have access to all of the code and embedded environment variables in your app. Read more about storing sensitive info.