expo: expo SDK 49 does not load custom env file

Minimal reproducible example

https://github.com/dooboolab-community/expo-router-starter/pull/5

Summary

In expo SDK 48, it was possible to load custom .env file with dotenv -e .env.prod -- expo start. However, from SDK 49, it does not work.

You may try working example in https://github.com/dooboolab-community/expo-router-starter main branch and not working example in https://github.com/dooboolab-community/expo-router-starter/pull/5.

I setup .env and .env.prod with below variables. For .env,

ROOT_URL=localhost:3000

For .env.prod

ROOT_URL=localhost:5050

SDK 48 results

  • yarn start Screenshot 2023-07-26 at 4 11 21 PM
  • yarn start:prod Screenshot 2023-07-26 at 4 11 46 PM

SDK 49 results

  • yarn start Screenshot 2023-07-26 at 4 13 28 PM

  • yarn start:prod Screenshot 2023-07-26 at 4 13 53 PM

Environment

expo-env-info 1.0.5 environment info:
  System:
    OS: macOS 13.4.1
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.15.0 - ~/.nvm/versions/node/v18.15.0/bin/node
    Yarn: 3.6.0 - ~/.nvm/versions/node/v18.15.0/bin/yarn
    npm: 9.7.2 - ~/.nvm/versions/node/v18.15.0/bin/npm
    Watchman: 2023.07.10.00 - /opt/homebrew/bin/watchman
  Managers:
    CocoaPods: 1.12.1 - /opt/homebrew/bin/pod
  SDKs:
    iOS SDK:
      Platforms: DriverKit 22.4, iOS 16.4, macOS 13.3, tvOS 16.4, watchOS 9.4
  IDEs:
    Android Studio: 2021.2 AI-212.5712.43.2112.8815526
    Xcode: 14.3.1/14E300c - /usr/bin/xcodebuild
  npmPackages:
    @expo/webpack-config: ^18.1.2 => 18.1.2 
    expo: ^49.0.5 => 49.0.5 
    react: 18.2.0 => 18.2.0 
    react-dom: 18.2.0 => 18.2.0 
    react-native: 0.72.3 => 0.72.3 
    react-native-web: ~0.19.7 => 0.19.7 
  npmGlobalPackages:
    eas-cli: 3.16.0
  Expo Workflow: managed

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Reactions: 1
  • Comments: 20 (7 by maintainers)

Commits related to this issue

Most upvoted comments

Would be very practical to allow for custom env files if possible. In our case, we have a staging environment for which we’d like to have a .env.staging but make it behave as if the NODE_ENV was production and also a qa environment with .env.qa. In our previous app, we had to use a hacky setup like proposed above but I’d much rather have something working out of the box for supporting custom environments other than development, test and production. Definitely a very nice step in the good direction for the environment variables with SDK 49 though.

I get a weird result as well the env vars work on ios / android but on web they are undefined 🤔 is there a solution for this ?

maybe totally unrelated but when I build expo run:ios --configuration Release it says the .env is loaded when first running command but they are undefined at run time. exact same command without --configuration Release works

edit:

After messing with .env some more it seems like it’s wayyy more trouble than it’s worth and I’m not convinced it’s actually stable. Seems to not work in some cases, makes things less type safe, behavior with eas update seems inconsistent. Also, when using CI it’s required to generate a .env file during the action which is a pain.

Maybe I’m just dumb, but all these problems can be easily solved by just hardcoding the environment variable values and switching based in the eas.json updates channel.

If you call the files .env.development and .env.production it works out of the box now in SDK 49.

Expo will load the production one automatically when you set NODE_ENV=production before you start the server. If you still need to load custom ENV files (even in EAS), with custom naming, hit me up. It’s possible but some changes are needed.