react-native-dotenv: Can't get multi env working

  • Asked question in discussions
  • Tried the troubleshooting Wiki
  • Followed the migration Wiki

Describe the bug I have multiple env files,

env.staging
env.production

I use the command APP_ENV=staging npx react-native run-ios to run the app. But the I get undefined when accessing variables

import {API_TOKEN} from "@env";
console.log(API_TOKEN);

To debug the issue I added a console log here react-native-dotenv/index.js Screenshot 2022-10-19 at 10 39 28

There is no APP_ENV variable in process.env I followed the documentation correctly and unable to figure out why APP_ENV not being passed.

For the moment I’m setting APP_ENV as a global env variable in .bash_profile and it works fine.

To Reproduce Steps to reproduce the behavior:

  1. Add multiple env files
  2. run the app APP_ENV=staging npx react-native run-ios
  3. Console log the environment variables in the env files

Expected behavior Correct environment variables should be set

Screenshots If applicable, add screenshots to help explain your problem.

Dev environment (please complete the following information):

  • react-native 0.68.3
  • react-native-dotenv 3.3.1

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 4
  • Comments: 15 (5 by maintainers)

Most upvoted comments

I know this issue has been marked as closed, but it’s the exact problem I am facing.

APP_ENV does not get passed down to the launched metro terminal when running the command react-native run-ios e.g. APP_ENV=staging react-native run-ios does not load the .env.staging file, it loads .env instead

But will get passed down when running react-native start e.g. APP_ENV=staging react-native start and the starting the app with i or a. The issue with doing it this way (at least for me) is I have multiple flavors I want to use, which are tricky to launch via the metro cli.

I don’t think the problem is with this lib, it feels like its more about how you set the APP_ENV var in a way metro can load and use it?

Same here. Basically, whenever I run react-native run-ios or react-native run-android, nor NODE_ENV or APP_ENV are passed down. It basically defaults to the .development env file and whatever values are in there. The only way it seems to be working in my local is by re-starting the packager and passing either NODE_ENV or APP_ENV there, as in:

NODE_ENV=staging yarn start

But starting the packager and running the app separately, like so:

yarn start
NODE_ENV=staging yarn react-native run-android

Doesn’t do the trick. Instead of picking up the passed down NODE_ENV, it defaults to .env (or the .development one).

Hey guys, I’ve found sort of a solution (honestly, I don’t know if it should be categorized as such). But, I tried:

  • Setting the APP_ENV variable at the beginning of the command npx react-native run-[android | ios]
  • Setting it on my .bash_profile or .zshrc
  • Changing the name through configuration on the plugin like { envName: 'MY_ENV' ], and,
  • Even setting and exporting the APP_ENV and NODE_ENV at the same time

And none worked for me. What actually worked was executing APP_ENV=test npm run start. I figure, Metro, running in a separate command line instance, cannot access the variable APP_ENV once it’s started running (which, I know, it’s weird, but considering that, for example, when you set a new variable in your .zshrc you need to restart your cmd session to “refresh” the env values, It kind of becomes logical).

So, setting it with the env we want actually spawns a new Metro instance with the APP_ENV variable set to the value we want. All we need now is to confirm that changing the envName also works this way (because I didn’t try that after I succeeded in my goal to have multiple env vars), and maybe update the documentation? (That’s why I don’t know if it’s a solution or if I just found a workaround).

Edit: I have metro.config.js with resetCache: true, and to change the environment (or see updates to the one you’re using) you need to restart Metro with the new env value set (sometimes the app is able to be refreshed after doing this, if not just rebuild it).

I’m facing the exact same issue even when I add theseresolutions and reset the cache nothing works 😦

Ok I see now. I’ll make sure to test the specific command