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

ENV variables are not loaded at runtime #199

Closed
vineetaparodkar opened this issue Jul 13, 2021 · 8 comments
Closed

ENV variables are not loaded at runtime #199

vineetaparodkar opened this issue Jul 13, 2021 · 8 comments
Assignees
Labels
wontfix This will not be worked on

Comments

@vineetaparodkar
Copy link

vineetaparodkar commented Jul 13, 2021

Describe the bug
ENV variables are not loaded at runtime. Old env variables are getting assigned in components and works if console logs are added.

To Reproduce
Steps to reproduce the behavior:

  1. Added module in babel.config.js
module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
  plugins: [
    ["module:react-native-dotenv"]
  ]
};
  1. Added .env file in root directory with following values, Allowed THEME values -->theme1/theme2 DEFAULT_MODE=yes/no
THEME="theme1"
DEFAULT_MODE="yes"
  1. Importing and using the env variables
import {THEME, DEFAULT_MODE} from "@env";
const AuthStack = createStackNavigator({
  Welcome: {
    screen: Welcome,
    navigationOptions: {
      title: 'APP NAME',
      headerShown: DEFAULT_MODE == "yes" ? true : false,
    }
  }})
  1. Scripts updated in package.json
    "cc": "rimraf node_modules/.cache/babel-loader/*,", "android": "npm run cc && react-native run-android",

Expected behavior
Above code should work after changing THEME="theme2" & DEFAULT_MODE="no" and rebuilding app using npm run android and headerShown value should be false

** Error**
Above code still points to DEFAULT_MODE="yes" and works i.e. headerShown value updates to false only if there is console statement after import in every file. i.e
import {THEME, DEFAULT_MODE} from "@env"; console.log("THEME CHECK",THEME, DEFAULT_MODE);

React native version used:
"react": "16.13.1",
"react-native": "0.63.3",

@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/199

@goatandsheep
Copy link
Owner

My guess is that babel recompiles the file when you make the change of adding the console.log . I don't know why but I advise you try to invalidate the cache in your babel config https://github.com/goatandsheep/react-native-dotenv-metro-demo/blob/c0daeebe35a87c524eb88803e19f445d6d6956d4/babel.config.js#L2

@v-ken
Copy link

v-ken commented Aug 26, 2021

Not sure why this isn't working for me either. But as you mentioned, if there is a console.log with that env var just before it's being used, it works. Adding a String cast also seems to work for me:

String(MY_ENV_VAR)

@stale
Copy link

stale bot commented Oct 25, 2021

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.

@stale stale bot added the wontfix This will not be worked on label Oct 25, 2021
@stale stale bot closed this as completed Nov 2, 2021
@Ryanjso
Copy link

Ryanjso commented Sep 7, 2022

has anyone figured out a more elegant solution to this?

@goatandsheep
Copy link
Owner

I'm working on using a new Babel feature that came out this year but it has not been widely used so documentation has been challenging to follow. Fix should be in next release, tracked in #75

@Thebenjamist
Copy link

I just solved the issue, need to run yarn start --reset-cache, then it runs like a dream

@goatandsheep
Copy link
Owner

Hi @Ryanjso this ticket is closed because I haven't been able to reproduce it since recent changes. Make sure your Babel version is up to date or if you're using Expo that you're using the latest version of that or if you're using something else that is forcing the version of Babel down that you use package resolutions

Repository owner locked and limited conversation to collaborators Apr 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

5 participants