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

Environment variable evaluation not working in index.html #5473

Closed
marco-gagliardi opened this issue Oct 18, 2018 · 6 comments
Closed

Environment variable evaluation not working in index.html #5473

marco-gagliardi opened this issue Oct 18, 2018 · 6 comments
Labels

Comments

@marco-gagliardi
Copy link

marco-gagliardi commented Oct 18, 2018

Is this a bug report?

yes

Did you try recovering your dependencies?

yes

Environment

System:
OS: macOS High Sierra 10.13.6
CPU: x64 Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz
Binaries:
Node: 10.9.0 - /usr/local/bin/node
npm: 6.2.0 - /usr/local/bin/npm
Browsers:
Chrome: 70.0.3538.67
Safari: 12.0
npmPackages:
react: ^16.5.2 => 16.5.2
react-dom: ^16.5.2 => 16.5.2
react-scripts: ^2.0.3 => 2.0.5
npmGlobalPackages:
create-react-app: Not Found

Steps to Reproduce

  1. Add a condition script in your public/index.html file based on an env variable:
<script>
      '%REACT_APP_ENV%' === 'production' ?  console.log('production') : console.log('not production')
</script>
  1. Set a value for that env variable at build time: REACT_APP_ENV=production react-scripts build
  2. Inspect build/index.html

Expected Behavior

You should see

<script>console.log('production')</script>

Actual Behavior

you see:

<script>console.log('not production')</script>

@jtwebb
Copy link

jtwebb commented Oct 18, 2018

I can't give a satisfying reason as to why this is happening, but I can give a fix.

<script>
      '%REACT_APP_ENV%'.toString() === 'production' ?  console.log('production') : console.log('not production')
</script>

I'm not sure why we would have to add toString() because if you check the types on both '%REACT_APP_ENV%' and 'production' they're both strings. And the values are both 'production'. But as soon as I added the toString() it worked as expected.

@marco-gagliardi
Copy link
Author

Maybe they are not at build time, when the condition is evaluated. Thanks for the temporary fix though!

@Poyoman39
Copy link

Poyoman39 commented Nov 7, 2018

+1 Have the same issue.

        console.log('%REACT_APP_CONFIGURATION_BEHAVIOR%');
        console.log('web');
        console.log('%REACT_APP_CONFIGURATION_BEHAVIOR%' == 'web');

got => web web false

It looks like the environements variables have differents values / are not set inside a build tool (probably webpack). Adding ".toString()" probably delay the evaluation of the expression at runtime

Note: I'm using "react-scripts": "2.1.1",

jefffriesen added a commit to RadiantLabs/microgrid-appliance-analysis that referenced this issue Nov 17, 2018
@stale
Copy link

stale bot commented Dec 8, 2018

This issue has been automatically marked as stale because it has not had any recent activity. It will be closed in 5 days if no further activity occurs.

@stale stale bot added the stale label Dec 8, 2018
@stale
Copy link

stale bot commented Dec 13, 2018

This issue has been automatically closed because it has not had any recent activity. If you have a question or comment, please open a new issue.

@stale stale bot closed this as completed Dec 13, 2018
@andreisocaciu
Copy link

console.log('%REACT_APP_LOAD_STUFF%' === 'true') still returns false.

@lock lock bot locked and limited conversation to collaborators Jan 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants