eas-cli icon indicating copy to clipboard operation
eas-cli copied to clipboard

Environment variables don't seem to be detected in build

Open JorensM opened this issue 1 year ago • 50 comments

Build/Submit details page URL

https://expo.dev/accounts/jorensm/projects/app/builds/3dcb9d5c-dfb2-487d-a6e5-8ce5c65b2288

Summary

So I'm trying to add environment variables to my EAS build. I've added them in my eas.json file, like so:

env vars

But they don't seem to be detected in my build. I have a validator function that throws an error if the env variables are not set, and its throwing it in my builds, thought it works fine with Expo Go.

Managed or bare?

Managed

Environment

expo-env-info 1.2.0 environment info: System: OS: Windows 10 10.0.19045 Binaries: Node: 20.8.0 - C:\Program Files\nodejs\node.EXE Yarn: 1.22.17 - ~\AppData\Roaming\npm\yarn.CMD npm: 10.1.0 - C:\Program Files\nodejs\npm.CMD SDKs: Android SDK: API Levels: 30, 31, 33 Build Tools: 30.0.2, 30.0.3 IDEs: Android Studio: Version 2020.3.0.0 AI-203.7717.56.2031.7678000 npmPackages: @expo/webpack-config: ^19.0.0 => 19.0.0 expo: ~49.0.15 => 49.0.21 expo-router: ^2.0.0 => 2.0.14 react: 18.2.0 => 18.2.0 react-dom: 18.2.0 => 18.2.0 react-native: 0.72.6 => 0.72.6 react-native-web: ~0.19.6 => 0.19.10 Expo Workflow: managed

expo-doctor didn't find any issues

Error output

No response

Reproducible demo or steps to reproduce from a blank project

This is the file that loads my env variables. On Expo Go it doesn't throw an error but on a EAS build it does.

// Constants from environment variables

const ENV_VARIABLE_NAMES = [
    'API_URL',
    'STRIPE_PUBLISHABLE_KEY',
    'SUPABASE_URL',
    'SUPABASE_KEY'
]

const validateEnvVariables = () => {
    const missing_vars = []
    for(const var_name of ENV_VARIABLE_NAMES) {
        const full_var_name = 'EXPO_PUBLIC_' + var_name
        if(!process.env[full_var_name]) {
            missing_vars.push(full_var_name)
        }
    }

    if (missing_vars.length > 0) {
        throw new Error('Missing the following environment variables: ' + missing_vars.join(', '))
    }
}

validateEnvVariables();

export const API_URL = process.env.EXPO_PUBLIC_API_URL!;
export const SUPABASE_URL = process.env.EXPO_PUBLIC_SUPABASE_URL!;
export const SUPABASE_KEY = process.env.EXPO_PUBLIC_SUPABASE_KEY!;
export const STRIPE_PUBLISHABLE_KEY = process.env.EXPO_PUBLIC_STRIPE_PUBLISHABLE_KEY!;

JorensM avatar Jan 22 '24 11:01 JorensM

Apparently Expo only supports dot notation for accessing env variables. Which is strange because it worked with Expo Go. I'll try changing the validator to use dot notation and report back.

JorensM avatar Jan 22 '24 11:01 JorensM

Update: After changing my validator to use dot notation, I'm still getting the same error about missing env vars

JorensM avatar Jan 22 '24 12:01 JorensM

When I run the build command it does show in the CLI that the env variables were added:

console output

Whereas in my EAS build logs, under Spin up build envrionment it doesn't show the env variables (though I'm not sure if it should)

JorensM avatar Jan 22 '24 12:01 JorensM

Hi there,

I wanted to check your build logs, but they are expired. Are you still facing this issue? If so may I ask you to run the build once again and send me the link?

szdziedzic avatar Feb 05 '24 20:02 szdziedzic

@szdziedzic I'm not working on this project anymore as my client decided to pivot, but I'll try to re run the build when I have some free time

JorensM avatar Feb 06 '24 10:02 JorensM

Can confirm this a real issue I am facing.

i-mighty avatar Feb 19 '24 16:02 i-mighty

I am having the same issue as well

ermamud avatar Feb 25 '24 18:02 ermamud

Also having the same issue except i can actually see them in the spin up build environment step. However when i publish my app, they are not there.

Screenshot 2024-03-01 at 09 36 06

EmilChigu avatar Mar 01 '24 09:03 EmilChigu

Hello was this solved. I have the same issue. It works fine in expo go but when I run a dev build they are all undefined.

pariah140 avatar Mar 19 '24 08:03 pariah140

Has anyone found a solution to this issue yet ?

Inalegwu avatar Mar 27 '24 04:03 Inalegwu

I had a similar issue, while doing local eas build. Everything looks to be working well with expo builds, but fails during eas local builds.

It appears that I needed to set the environment variables for the execution of build command. So, here is what I did -

  1. created a bash script set-env.sh (It reads your .env file, and sets the environment)
#!/bin/bash

if [ $# -lt 1 ]; then
  echo "Usage: $0 <command>"
  exit 1
fi

(env $(cat .env | xargs) "$@")

  1. Run - sh set-env.sh eas build --profile test --platform ios --local

anup-a avatar Mar 28 '24 07:03 anup-a