Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't get multi env working #365

Closed
3 tasks done
malwatte opened this issue Oct 19, 2022 · 15 comments
Closed
3 tasks done

Can't get multi env working #365

malwatte opened this issue Oct 19, 2022 · 15 comments
Assignees

Comments

@malwatte
Copy link

  • 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
@github-actions
Copy link

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/365

@goatandsheep
Copy link
Owner

So when you set app env in the bash profile your process env prints the correct value for app env?

Sorry I'm just trying to understand what happened here.

I'm trying to figure out if it's a issue with how the env var was set https://www.npmjs.com/package/dotenv-flow#node_env--specific-env-files

Or if it's being overwritten somehow?

Or it's another caching issue? (this will be worked on next week!!)

Or something else?

@malwatte
Copy link
Author

Yes. When I set the APP_ENV in bash profile it correctly prints the values of correct .env file and I see APP_ENV is one of the process.env variables (when I add a console log in react-native-dotenv/index.js line 40)

I even tried with a different name e.g. MY_APP_ENV by changing the envName in the babel.config.js and running MY_APP_ENV=staging npx react-native run-ios. But it did not work either.

To make sure anything not cached I set resetCache: true in metro.config.js

Also,

If I run other scripts those gets APP_ENV correctly.
e.g.

test.js

const process = require('process');
const env = process.env.MY_APP;
console.log(env);

MY_APP=staging node ./test.js

When I run npx react-native run-ios metro server runs in a different shell. Can that be the issue?

@goatandsheep
Copy link
Owner

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

@soemarko
Copy link

I'm facing the same problem, as per #385 discussion. Any update on this? I thought I missed something obvious since it seems like it just works for others. And the questions are for edge cases.

@goatandsheep
Copy link
Owner

goatandsheep commented Dec 7, 2022

Hi I tried this in my test project and without caching resolved, it failed to work correctly, but with it resolved it worked correctly. To resolve caching issues, please ensure that you are using react-native-dotenv 3.4.2 or later and if the following PR is not closed ( expo/expo-cli#4593 ) you will need to add the following to your package.json:

"resolutions": {
  "@babel/core": "^7.20.2",
  "babel-loader": "^8.3.0"
}

This will be added to the README soon. If it is closed, please make sure to update expo and expo webpack config to whatever version that has been released unto.

If the above suggestions do not work let me know and we will continue investigating.

@abdoutech19
Copy link

abdoutech19 commented Feb 15, 2023

I'm facing the exact same issue even when I add theseresolutions and reset the cache nothing works :(

@Victor-ARC
Copy link

Victor-ARC commented Sep 27, 2023

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).

@goatandsheep
Copy link
Owner

goatandsheep commented Sep 28, 2023

Hi @Victor-ARC thank you so much for this. I have not been able to reproduce this issue but if you have a reproduction instance I'd really appreciate it so i can update the documentation or tweak the code to figure out why this is happening and fix it. I'm sorry, it shouldn't be this difficult to use.

@Victor-ARC
Copy link

Victor-ARC commented Sep 28, 2023

Hi @goatandsheep
I'll try to get a working reproduction snack for you over the weekend.

But, technically, you should just need a basic RN app and try to add the RN-dotenv lib. Maybe you could try doing this on a different machine than yours? My guess is that your usual machine is already set with the variables and stuff. Could be that this happens because we're not in your environment and thus our Metro instance can't communicate with the APP_ENV variable like yours (which I know sounds unlikely and possibly nuts, but hey, we lose nothing if we try, right?)

@DanielPritchard
Copy link

DanielPritchard commented Oct 11, 2023

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?

@fellenabmb
Copy link

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).

@matheusjardimb
Copy link

Hello there.

It seems the issue is still happening (I can't get my app building with 3 different .env files) but the issue was closed without a solution, right?

In my case, I have .env, .env.staging and .env.production. I can get the app to load .env and .env.production, but not the .env.staging.

Any tips on this one?

@parasfusemachine
Copy link

Any solution?
I am also having same issue with 3 .env files, .env, .env.staging and .env.production.

@goatandsheep
Copy link
Owner

Hi sorry I don't know what the problem is but I'm confident if you tell me what you've tried I can get you a solution. Unfortunately you can't use a snack because snack doesn't let you install devDependencies and this library pretty much only works as a devDependency. If you tell me the exact line you're using to run your code I can help you :) For example, @fellenabmb used

NODE_ENV=staging yarn react-native run-android

you only need to use the run-android command once since that builds the android output. After that you can use start and it will update your android code automatically. The reason why you need to use start is because it supports the --reset-cache flag.

Also, I'm noticing the toolchains are more robustly overwriting the value of NODE_ENV. I have figured out some hacks, but the simplest thing to do is use APP_ENV.

yarn react-native run-android
APP_ENV=staging yarn react-native start --reset-cache

I hope that helps for now!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants