eas.json environment variables are undefined in app.config.js during local development

Hello everybody!

Im trying to use eas.json as a main config file for environment variables. My setup:

eas.json:

{
  "cli": {
    "version": ">= 3.8.0"
  },
  "build": {
    "development-simulator": {
      "developmentClient": true,
      "distribution": "internal",
      "ios": {
        "simulator": true
      },
      "env": {
        "API_URL": "www.foo.com"
      }
    },
   [...] //the rest of the file
}

app.config.js:

export default () => {
  console.log("MY ENV:", process.env.API_URL);
  return {
    expo: {
      [...] // the beginning of the object;
      extra: {
        eas: {
          projectId: {myProjectID},
        },
        apiUrl: process.env.API_URL,
      },
    },
  };
};

I can see that after eas build --profile development-simulator --platform ios the console logs: MY ENV: "www.foo.com" a couple of times.

After the build completes and I install the app on my local simulator and start it using npx expo start --clear, now the log is: MY ENV: undefined.

In the code when i use Constants.expoConfig?.extra?.apiUrl i also get undefined in return.

Am i doing something wrong?

Hi @luckywacky

Unfortunately, that’s expected.

You will either need to specify it on the command line, e.g.:

API_URL="http://127.0.0.1:3000" npx expo start

Or you will need to have some other mechanism to load the environment variables during development. e.g. Using Babel to inline environment variables during build time or Loading environment variables from a file

Thank You for your answer @wodin !

Using Babel to inline environment variables during build time

Isn’t this solution for swapping environment variables during build time (local builds) rather than live development ? The name would certainly suggest that.

Nevertheless - I have downloaded the plugin, set it up according to docs, rebuild the app with eas build --profile development-simulator --platform ios, but still after npx expo start --clear the app launches and the process.env.API_URL is undefined.

Loading environment variables from a file

As far as i know there is no way to keep those files in sync with eas.json, is there?


I found this section in the docs about the issue i am facing. However - even though the title might suggest otherwise - this setup after npx expo start will still result in process.env.API_URL to be undefined.

Is the whole idea of eas.json and app.config.js environment variables only viable for build time? Do i have to use .env or .rcenv or API_URL="http://foo.com" npx expo start to have those variables available during local development ?

Is the whole idea of eas.json and app.config.js environment variables only viable for build time?

eas.json is a configuration file for eas services, when you are developing locally you are not using eas. This is the same situation as in any other ci service, you usually have a separate set of envs fro staging/production and for development.

If you have a lot of envs common to what you build on eas and for local development I recommend puting all those values in your js code e.g.

const configs = {
  local: {
     ....
  production:...
  staging: ...
}
function getConfig(environment: string): Config {
   return configs[envirionment]
}

and just use one env to pick which config should be used. This way you only need to specify one env in eas.json or in shell when running expo start or expo update

1 Like

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.