Environment variables in Expo projects
July 3, 2020
TL;DR
- Keine Secrets in den App Bundle einbauen.
- Env Werte in
app.config.jsundextrabereitstellen. - EAS Build Variablen pro Environment setzen.
app.config.js (empfohlen)
code
export default ({ config }) => {
const env = process.env.APP_ENV || "dev";
return {
...config,
extra: {
appEnv: env,
apiUrl: env === "prod" ? "https://api.example.com" : "https://api-dev.example.com",
},
};
};
Im Code:
code
import Constants from "expo-constants";
const { apiUrl } = Constants.expoConfig?.extra ?? {};
EAS Build Variablen
code
APP_ENV=staging npx eas build -p android
Oder per eas.json profiles:
code
{
"build": {
"staging": {
"env": {
"APP_ENV": "staging"
}
}
}
}
dotenv
.env ist fuer lokale Devs ok, aber alles landet im Bundle, wenn du es einbindest.
Nutze es nie fuer Secrets.
Security Hinweis
Alles, was in den App Code geht, ist auslesbar. Secrets nur im Backend/CI.