Skip to content

Incompatible with expo-router #501

Closed

Description

@brentvatne

Describe the bug

Cross-posting from the Expo repository: expo/expo#28933

So what's happening here is that react-native-dotenv is agressively inlining all environment variables from the machine in the bundle. This has 2 side effects that cause the behavior we are seeing:
It precedes the babel-preset-expo/src/expo-router-plugin.ts transformation, causing important settings of Expo Router to be overwritten. (this file)
In the system environment (outside the bundle), there is an environment variable with the same name used as the require.context within Expo Router. It's from the type generation, and uses the absolute path we are seeing here (generated here, set as process.env here)

react-native-dotenv seems to do some internal evaluation (possibly due to static rendering, since we evaluate bundle code). Even when setting safe or allowlist / blocklist, it seems to pollute/mutate all environment variables

To Reproduce
Steps to reproduce the behavior:

  1. Run npx create-expo-app
  2. cd my-app
  3. npm install react-native-dotenv
  4. Add the react-native-dotenv babel plugin
  5. Boot the app with npx expo start --ios
  6. Notice that you see "Welcome to Expo" screen rather than the routes in /app (without the plugin, you will see tabs and the app/(tabs)/index.tsx screen.

https://github.com/brentvatne/repro-dotenv

Expected behavior
react-native-dotenv should play nicely with expo-router.

Screenshots
n/a

Desktop (please complete the following information):

  • OS: any
  • Browser: any
  • Version: any

Smartphone (please complete the following information):

  • Device: any
  • OS: any
  • Browser: any
  • Version: any

Additional context
Given this incompatibility, we are currently recommending that developers using Expo Router use Expo CLI's built-in environment variable support.

I think that one way to fix this issue would be to change the default behavior to whitelisted env vars only. In my opinion, not having this default can be a footgun.

Activity

  1. github-actions commented on May 16, 2024

    @github-actions

    Hey, thank you for opening this issue! 🙂 To boost priority on this issue and support open source please tip the team at https://issuehunt.io/r/goatandsheep/react-native-dotenv/issues/501

  2. byCedric commented on May 16, 2024

    @byCedric

    To provide some more context, Expo uses internal environment variables that are not considered public (and therefore should never end up in the bundle). If these variables can be left untouched, we should have a way to get Expo Router and react-native-dotenv to work together.

    Basically, all EXPO_* variables should be left alone, with the gray area being EXPO_PUBLIC_* variables. This is mostly defined by users using the default .env integration in Expo.

  3. goatandsheep commented on May 17, 2024

    @goatandsheep
    Collaborator

    Does it work if you add these to blocklist? I'm happy to add this hint to docs if it works.

  4. goatandsheep commented on May 17, 2024

    @goatandsheep
    Collaborator

    This babel plugin is meant to be simple and non specific. React native dotenv is meant to encourage best practices and you're welcome to go around this using blocklist. Expo-variables is a great alternative, but I'm not sure this is a fault of the library just yet. If you can provide a sample I'd be happy to take a look.

  5. goatandsheep commented on May 17, 2024

    @goatandsheep
    Collaborator

    My bad I didn't see you provided a repro project. I'm taking a look now

  6. goatandsheep commented on May 17, 2024

    @goatandsheep
    Collaborator

    I also didn't read that blocklist didn't work for you. That is concerning.

  7. byCedric commented on May 17, 2024

    @byCedric

    Yeah, I'm not exactly sure what's going on. In Expo, we do some bundling and possible static rendering (basically evaluating parts of bundle code in Node). Not sure if that might have anything to do with it. So far, neither safe + path, blocklist, or allowlist worked for me, unclear why.

    It would be nice if this library can work in Expo, if users need more control over it.

  8. danicunhac commented on May 22, 2024

    @danicunhac

    I'm having the same problem and since my project is still under development I've unshifted EXPO_PUBLIC_ to my env variables, as specified in https://docs.expo.dev/guides/environment-variables/.

  9. mayel15 commented on Jul 2, 2024

    @mayel15

    I was facing the same problem. FYI, the incompatibility of react-native-dotenv with expo-router can load a default screen of expo, and not your normal dev app you're building.

    Simulator Screenshot - iPhone SE (3rd generation) - 2024-07-01 at 22 22 23

    As an alternative for react-native-dotenv, they're the default Expo CLI's built-in environment variable support or react-native-config for bare react native app.

  10. a-eid commented on Jul 3, 2024

    @a-eid

    @mayel15 unfortunately expo's built in env variables is not ideal, rn dotenv is way better.

  11. brentvatne commented on Jul 16, 2024

    @brentvatne
    Author

    @a-eid - can you explain the limitations that you're encountering? you could try disabling them, i'm not sure if that will help here though: https://docs.expo.dev/guides/environment-variables/#disabling-environment-variables

  12. a-eid commented on Jul 17, 2024

    @a-eid

    @brentvatne in expo we don't have the ability to load arbitrary .env files. for example .env.qa or .env.staging you can only load standard .env files .env.production, .env.test or .env.development.

    however with RN dotenv, we're able to do so use the APP_ENV env variable.

  13. robin-scharf commented on Sep 2, 2024

    @robin-scharf

    I was facing the same problem. FYI, the incompatibility of react-native-dotenv with expo-router can load a default screen of expo, and not your normal dev app you're building.

    Simulator Screenshot - iPhone SE (3rd generation) - 2024-07-01 at 22 22 23

    As an alternative for react-native-dotenv, they're the default Expo CLI's built-in environment variable support or react-native-config for bare react native app.

    +1 Fortunately, I was able not to depend on react-native-dotenv.

  14. cchoi94 commented on Sep 10, 2024

    @cchoi94

    +1 here, our app is dependent on this package for reasons outlined by @a-eid. Is there something in the works for a fix?
    Would love to update to expo 51 asap.

  15. 2 remaining items

  16. tiavina-mika commented on Jan 14, 2025

    @tiavina-mika

    @cchoi94 's solution works, thanks.
    When will it be implemented in the main repository?

  17. goatandsheep commented on Jan 14, 2025

    @goatandsheep
    Collaborator

    I am reading this now. Will review and help with merge today

  18. goatandsheep commented on Jan 14, 2025

    @goatandsheep
    Collaborator

    Ok i took a look at the code. Will require minor cleanup but overall looks awesome. Will be merging in today.

  19. goatandsheep commented on Jan 14, 2025

    @goatandsheep
    Collaborator

    After closer analysis, there are some fundamental concerns that this will cause errors. I will have to spend more time on the weekend figuring out how to fix this

  20. samuelpeybernes-dev commented on Jan 26, 2025

    @samuelpeybernes-dev

    Is there some news about the fix implementation in the main repository?

  21. david-bermejo commented on Mar 10, 2025

    @david-bermejo

    Hi @goatandsheep, have you had a chance to spend more time on this issue? It's fantastic, and I’d love to continue using it alongside expo-router

  22. stale commented on Jun 27, 2025

    @stale

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

  23. github-actions commented on Jul 23, 2026

    @github-actions

    Hey, thank you for opening this issue! 🙂 To boost priority on this issue and support open source please tip the team at https://issuehunt.io/r/goatandsheep/react-native-dotenv/issues/501

  24. added a commit that references this issue on Jul 28, 2026
  25. motdotla commented on Jul 28, 2026

    @motdotla
    Collaborator

    Hey everyone,

    This is fixed in v4.1.0 and greater. Thank you for using react-native-dotenv!

    cc @brentvatne @david-bermejo @samuelpeybernes-dev @tiavina-mika @mayel15 @byCedric

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions