skip to Main Content

I have a .env file:

REACT_APP_URL={value}
REACT_APP_API_URL={value}
REACT_APP_VEHICLE_IMAGE_URL={value}
REACT_APP_NAME={value}
REACT_APP_GA_TRACKING_ID={value}
REACT_APP_WEBSITE_URL={value}

That is my current file of eas.json:

{
  "cli": {
    "version": ">= 10.0.2"
  },
  "build": {
    "development": {
      "developmentClient": true,
      "distribution": "internal",
      "ios": {
        "simulator": true
      }
    },
    "preview": {
      "distribution": "internal"
    },
    "production": {}
  },
  "submit": {
    "production": {}
  }
}

I have stored these values in eas secret by running the command:

eas secret:push --scope project --env-file .env

I have stored on env some secret but the problem is when I run the app the env values are not stored in the build

eas:secret

when i run eas build --platform android --non-interactive --no-wait

I get this:

Loaded "env" configuration for the "production" profile: no environment variables specified

How can i load the env from the eas secret I have added, so when i build i can run the application with those variables?

2

Answers


  1. Expo environment variables

    Actually, your .env file should contain the variables like the following:

    EXPO_PUBLIC_API_URL=https://staging.example.com
    EXPO_PUBLIC_API_KEY=abc123
    

    And inside your codes, you can use the variables like the following:

    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" />;
    }
    

    For more information about using Expo environment variables you can read this link.

    HINT: Use the exact phrase process.env.EXPO_PUBLIC_XXX to get the variable from the .env file. The _XXX is your custom name.


    EAS Build

    To have a correct EAS Build you should write the eas.json file like the following:

    {
      "build": {
        "development": {
          "env": {
            "EXPO_PUBLIC_API_URL": "https://api.development.com"
          }
        },
        "production": {
          "env": {
            "EXPO_PUBLIC_API_URL": "https://api.production.com"
          }
        },
        "test": {
          "env": {
            "EXPO_PUBLIC_API_URL": "https://api.test.com"
          }
        }
      }
    }
    

    To have a better understanding of EAS Build configuration you can have a look at this link.

    Login or Signup to reply.
    1. Store the secrets in eas secrets

    2. Update your eas.json like this

       "production": {
       "env": {
           "EXPO_PUBLIC_YOUR_KEY_NAME": "EXPO_PUBLIC_YOUR_KEY_NAME"
       }
      

      }

    3. On eas build –platform ios. Ensure it says

      Loaded "env" configuration for the "production" profile:

    4. Test and ensure it works

    I was struggled to get the env injection working too. This ensure safety for the env variable. Hope this helps

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search