Expo多环境EAS构建异常:环境变量未正确加载
Expo EAS多环境构建环境变量异常排查问题
基础信息
- 工作流:
managed - eas-cli版本:
eas-cli/15.0.12 darwin-arm64 node-v22.13.0
已执行操作
- 在Expo平台创建环境变量
- 本地执行
eas env:pull拉取变量,生成的.env.local内容正确 - 在
app.config.ts中基于APP_VARIANT环境变量配置三个应用变体,代码如下:
import { ConfigContext, ExpoConfig } from 'expo/config' const colorGray200 = '#ECEDEE' const colorGreenDefault = '#003D4F' type TEnvironment = 'development' | 'preview' | 'production' interface TAdaptiveIcon { foregroundImage?: string monochromeImage?: string backgroundImage?: string backgroundColor?: string } interface TSplashScreenConfig { backgroundColor: string image: string dark?: { image: string backgroundColor: string } } interface AppEnvironmentConfig { name: string identifier: string icon: string adaptiveIcon: TAdaptiveIcon splash: TSplashScreenConfig } type AppConfig = Record<TEnvironment, AppEnvironmentConfig> const appConfig: AppConfig = { development: { name: 'MyApp Dev', identifier: 'app.myapp.be.dev', icon: './assets/icon.png', adaptiveIcon: { foregroundImage: './assets/adaptive-icon.png', backgroundColor: '#ffffff', }, splash: { backgroundColor: colorGray200, image: './assets/splash-icon.png', dark: { image: './assets/splash-icon-dark.png', backgroundColor: colorGreenDefault, }, }, }, preview: { name: 'MyApp Preview', identifier: 'app.***.be.preview', icon: './assets/icon.png', adaptiveIcon: { foregroundImage: './assets/adaptive-icon.png', backgroundColor: '#ffffff', }, splash: { backgroundColor: colorGray200, image: './assets/splash-icon.png', dark: { image: './assets/splash-icon-dark.png', backgroundColor: colorGreenDefault, }, }, }, production: { name: 'MyApp', identifier: 'app.***.be', icon: './assets/icon.png', adaptiveIcon: { foregroundImage: './assets/adaptive-icon.png', backgroundColor: '#ffffff', }, splash: { backgroundColor: colorGray200, image: './assets/splash-icon.png', dark: { image: './assets/splash-icon-dark.png', backgroundColor: colorGreenDefault, }, }, }, } export default ({ config }: ConfigContext): ExpoConfig => { const appVariant: TEnvironment = process.env.APP_VARIANT if (!appVariant) { throw new Error(`APP_VARIANT environment variable not defined`) } const currentAppConfig = appConfig[appVariant] return { ...config, name: currentAppConfig.name, slug: '****', owner: '****', version: '1.0.0', scheme: currentAppConfig.identifier, orientation: 'portrait', icon: currentAppConfig.icon, userInterfaceStyle: 'light', newArchEnabled: true, ios: { icon: { dark: './assets/images/ios-dark.png', light: './assets/images/ios-light.png', tinted: './assets/images/ios-tinted.png', }, bundleIdentifier: currentAppConfig.identifier, infoPlist: { ITSAppUsesNonExemptEncryption: false, }, }, android: { adaptiveIcon: currentAppConfig.adaptiveIcon, }, web: { favicon: './assets/favicon.png', }, plugins: [ 'expo-router', 'expo-secure-store', [ 'expo-splash-screen', { ...currentAppConfig.splash, imageWidth: 200, }, ], ], extra: { router: { origin: false, }, eas: { projectId: '0ffea***-****-****-****-**********8d', }, }, } }
eas.json配置如下:
{ "build": { "development": { "env": { "APP_VARIANT": "development" }, "environment": "development", "developmentClient": true, "distribution": "internal" }, "preview": { "env": { "APP_VARIANT": "preview" }, "distribution": "internal", "environment": "preview" }, "production": { "env": { "APP_VARIANT": "production" }, "environment": "production" } }, "cli": { "appVersionSource": "remote" } }
异常情况
执行所有EAS命令(如eas build、eas env:pull等)时,均出现以下错误:
Failed to read the app config from the project using "npx expo config" command: npx expo config --json exited with non-zero code: 1. Falling back to the version of "@expo/config" shipped with the EAS CLI.
但自行单独执行npx expo config时,可正常输出开发环境的配置信息。
疑问
- 在
eas.json中配置APP_VARIANT是否属于冗余操作? - 当前配置还缺失哪些必要步骤?
内容的提问来源于stack exchange,提问作者Sander Peeters
相关产品推荐
相关产品推荐

