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

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:
- Add multiple env files
- run the app
APP_ENV=staging npx react-native run-ios - 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)
Same here. Basically, whenever I run
react-native run-iosorreact-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 startBut starting the packager and running the app separately, like so:
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:
APP_ENVvariable at the beginning of the commandnpx react-native run-[android | ios].bash_profileor.zshrc{ envName: 'MY_ENV' ], and,APP_ENVandNODE_ENVat the same timeAnd 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 variableAPP_ENVonce it’s started running (which, I know, it’s weird, but considering that, for example, when you set a new variable in your.zshrcyou 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_ENVvariable set to the value we want. All we need now is to confirm that changing theenvNamealso 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.jswithresetCache: 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 these
resolutionsand reset the cache nothing works 😦Ok I see now. I’ll make sure to test the specific command