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

.env is not being loaded on eas update

Open matart15 opened this issue 2 years ago • 18 comments

Build/Submit details page URL

No response

Summary

I have .env file. it is working without problem on expo dev.

Screenshot 2024-01-11 at 11 10 55

But when i use expo update

Screenshot 2024-01-07 at 15 02 03

Managed or bare?

managed

Environment

Need to install the following packages: [email protected] Ok to proceed? (y)

expo-env-info 1.0.5 environment info: System: OS: macOS 14.1.2 Shell: 5.9 - /bin/zsh Binaries: Node: 20.10.0 - ~/.nvm/versions/node/v20.10.0/bin/node npm: 8.19.4 - ~/workplace/github/.../node_modules/.bin/npm Managers: CocoaPods: 1.11.3 - /usr/local/bin/pod SDKs: iOS SDK: Platforms: DriverKit 23.2, iOS 17.2, macOS 14.2, tvOS 17.2, watchOS 10.2 IDEs: Android Studio: 2021.3 AI-213.7172.25.2113.9123335 Xcode: 15.1/15C65 - /usr/bin/xcodebuild Expo Workflow: managed

npx expo-env-info 5.87s user 2.57s system 62% cpu 13.478 total

Error output

No response

Reproducible demo or steps to reproduce from a blank project

  1. create project
  2. set secrets
  3. create eas update
  4. add some code to see process.env ( if my understanding is correct secrets should be there )
<Pressable
              onPress={() => {
                Alert.alert("", JSON.stringify(process.env));
              }}
            >
  1. update pnpx eas-cli update --branch preview --message "update try"

matart15 avatar Jan 11 '24 02:01 matart15

What Expo SDK version are you using?

szdziedzic avatar Jan 11 '24 12:01 szdziedzic

package.json

 "expo": "49.0.18",

pnpm expo -v

0.10.15

matart15 avatar Jan 12 '24 02:01 matart15

Looking at your screenshot from https://github.com/expo/eas-cli/issues/2170, I see a warning about EAS CLI being installed as your project dependency. Can you remove it from your project dependencies and install the latest version globally?

szdziedzic avatar Jan 12 '24 08:01 szdziedzic

ok. removed it from package.json image

then tried pnpx eas-cli update --branch preview --message "update try"

results are same.

Even if i try NODE_ENV=preview; pnpx eas-cli update --branch preview --message "update try"

it shows only { NODE_ENV: "production" }.

matart15 avatar Jan 12 '24 09:01 matart15

Just in case :

❯ pnpx eas-cli -v
eas-cli/6.0.0 darwin-x64 node-v20.10.0
pnpx eas-cli -v  5.15s user 3.99s system 122% cpu 7.489 total

matart15 avatar Jan 12 '24 09:01 matart15

EAS Update does not pull in values from secrets. Secrets are not intended for updates! Secrets are values that will be present on an EAS Build worker temporarily to hold values like sensitive API keys to upload sourcemaps to Sentry and such things. You do not want to include these values in your update.

brentvatne avatar Jan 16 '24 18:01 brentvatne

@brentvatne

One thing I don't understand is

If i write app that uses process.env.MY_SECRET_API_KEY to access endpoints and eas build it will work.

Then I found small problem and runs eas update my app return undefined on process.env.MY_SECRET_API_KEY and breaks? doesn't this make eas update meaningless?

matart15 avatar Jan 17 '24 00:01 matart15

hey @matart15 , the problem is the way you are calling the environment variable. When eas update is run, we inline all your env var declarations from .env into the bundle. For example, if your code references process.env.EXPO_PUBLIC_TEST, we replace it with the value referenced in your .env file. This is why when you print out process.env, you'll get an empty object. But if you reference it using JavaScript's dot notation like process.env.EXPO_PUBLIC_TEST, you should see be able to see it.

Here are more details: https://docs.expo.dev/guides/environment-variables/#how-to-read-from-environment-variables

Note: I just tested this behaviour in SDK 50 to make sure it still works, but i see you are on SDK 49. If you are still getting the same issue, please let us know!

quinlanj avatar Jan 19 '24 22:01 quinlanj

Woow. I missed this line on the docs.

Alternative versions of the expression are not supported. For example, process.env['EXPO_PUBLIC_KEY'] or const {EXPO_PUBLIC_X} = process.env is invalid and will not be inlined.

Typescript linter gives me error if i use dot notation. I ignore linter and try it again with dot notation

matart15 avatar Jan 20 '24 03:01 matart15

I'm testing this now and also noticed that my EXPO_PUBLIC_API_URL=https://prod.api that was built with eas production was replaced with my local .env that i use EXPO_PUBLIC_API_URL=http://localhost:3000

I could see this as a problem when lets say i find a bug and i want to fix it fast but i forget to change .env file from Dev to Prod. so that means Dev URL will be pushed.

Also another issue, even if i pushed .env file with prod url i was getting issues still tho on main screen i have it printed and it does show the prod url but issue is that its not able to connect, i can't debug the production build but maybe i need create a simple example to test.

MariuzM avatar Mar 26 '24 19:03 MariuzM

I think i found solution

  1. Create .env.production file
  2. Only use the PROD public env's
  3. Run update like so NODE_ENV=production eas update --branch production --message "test 1" -p ios

Update: nope still having this weird issue where i can't make API requests even tho variable is PROD, doing build works fine, only update that is problem

MariuzM avatar Mar 26 '24 19:03 MariuzM

Update: there is some issue with Axios.

image
  • It started working, so ok i then made a build and pushed to app store, updated my phone with that all good.
  • Then i made expo update and broke again.
  • Made another expo update still nothing
  • Ok now i added same change (Alert) boom started working...
  • Removed that Alert and done Update all good still working...
  • But I'm sure if i do production build and push to test flight on the next expo update it will break i have no idea why, i feel like maybe when doing Expo Update, Axios needs to have some sort of Native component inside like this Alert? I just can't explain this sorry

My expo is configured like this image

and then just couple interceptors but they don't do much

MariuzM avatar Mar 26 '24 19:03 MariuzM

Update: there is some issue with Axios.

image * It started working, so ok i then made a build and pushed to app store, updated my phone with that all good. * Then i made expo update and broke again. * Made another expo update still nothing * Ok now i added same change (Alert) boom started working... * Removed that Alert and done Update all good still working... * But I'm sure if i do production build and push to test flight on the next expo update it will break i have no idea why, i feel like maybe when doing Expo Update, Axios needs to have some sort of Native component inside like this Alert? I just can't explain this sorry

My expo is configured like this image

and then just couple interceptors but they don't do much

I'm experiencing this same issue, you can also replicate this in a local app dev (prebuild) and expo go.

In my case, we have 2 separate API url for prod and dev, if I try to run prod by adding APP_ENV=production at the start of dev server, the app loads contents from dev API. Then I tried adding console logs in my axios.ts file and it now suddenly works.

This could probably be a problem in axios. I also can't explain why.

UPDATE:

This is not a problem with axios, it's the way expo loads env variables. You need to add --clear-cache command when running eas update

redkean avatar Apr 13 '24 02:04 redkean

The --clear-cache was a lifesaver. Wasn't able to figure out why updating an EXPO_PUBLIC_ environment variable in the relevant .env file wouldn't update after pushing an update, even after trying to push a small code change as well to attempt to force the change recognition. Would it make sense to include a one liner about this in https://docs.expo.dev/eas-update/environment-variables/?

evansendra avatar May 15 '24 22:05 evansendra

--clear-cache solved the issue, and i think it should be better for expo update to use the variables set in the production profile or in secrets instead of going with .env direclty

ahmed-khlifi avatar Oct 01 '24 09:10 ahmed-khlifi

This is a shit show. How many devs do you think have unknowingly pushed development environment configs to prod because of this? Why is this not documented CLEARLY somewhere?

And why does every single expo/eas service pull from/use environment variables differently? Seriously, what the fuck?

Joshuapwilley avatar Jun 20 '25 20:06 Joshuapwilley

It was pretty bad for us and sometimes clear cache wouldn't help so I wrote a script to handle it.

const fs = require('fs');
const { spawn } = require('child_process');
const path = require('path');
const yargs = require('yargs/yargs');
const { hideBin } = require('yargs/helpers');

// Parse command-line arguments
const argv = yargs(hideBin(process.argv))
  .option('channel', {
    alias: 'c',
    type: 'string',
    describe: 'Specify the channel',
    demandOption: true,
  })
  .option('message', {
    alias: 'm',
    type: 'string',
    describe: 'Message for the update',
    default: '',
  })
  .option('clear-cache', {
    type: 'boolean',
    describe: 'Clear the cache',
    default: true,
  }).argv;

// Load the configuration file
const configFilePath = path.join(__dirname, 'eas.json');
const configData = JSON.parse(fs.readFileSync(configFilePath, 'utf8'));

// Extract environment variables for the specified channel
const channelConfig = configData.build[argv.channel] || {};
let envConfig = channelConfig?.env || {};

if (channelConfig.extends) {
  const extendsConfig = configData.build[channelConfig.extends]?.env || {};
  envConfig = { ...extendsConfig, ...envConfig };
}

Object.keys(envConfig).forEach((key) => {
  process.env[key] = envConfig[key];
});

// Construct the `eas update` command
const easCommand = `eas update --channel ${argv.channel} --json --non-interactive`;

if (argv.message) {
  easCommand += ` --message "${argv.message}"`;
}

if (argv['clear-cache']) {
  easCommand += ' --clear-cache';
}

// Execute the command
const child = spawn(easCommand, { env: process.env, shell: true, maxBuffer: 1024 * 1024 });

child.stdout.on('data', (data) => {
  console.log(data.toString());
});

child.stderr.on('data', (data) => {
  console.error(`stderr: ${data}`);
});

child.on('close', (code) => {
  console.log(`child process exited with code ${code}`);
});

It loads in the eas.json file from the repo, chooses the specified channel and spawns the eas update command with the relevant process variables. Not perfect by any stretch, but has saved us some heartache. Plus it's allowed us to do some cool things like provide update messages to the user when there is a new update.

jarredwitt avatar Jul 09 '25 17:07 jarredwitt

FYI google brought me here ... if you're having this issue, read this: https://docs.expo.dev/eas/environment-variables/#eas-update?redirected and make sure you have your environment variables set up in the expo.dev portal. It won't pull from .env files (or didn't for me).

bstrackany avatar Jun 19 '26 17:06 bstrackany