-1

Can I use a variable in my process.env call?

I have the code

const optionsLang = `${lang}LANGOPTIONS`.replace("-","").toUpperCase();

and the .env file has

REACT_APP_ENUSLANGOPTIONS = {"title" : "applicationTitle", "buttonVerbiage":"Login Now"}
REACT_APP_ENCALANGOPTIONS = {"title" : "applicationTitle", "buttonVerbiage":"Login Now"}
REACT_APP_FRCALANGOPTIONS = {"title" : "applicationTitle", "buttonVerbiage":"Ouvir Une Session"}
REACT_APP_ESMXLANGOPTIONS = {"title" : "applicationTitle", "buttonVerbiage":"Iniciar Sesi%C3%B3n"}
and so on with various languages

so how can I do something like process.env.${optionsLang} (since that doesn't work)?

JaeEmAre
  • 7
  • 2
  • 1
    Does this answer your question? [Dynamically access object property using variable](https://stackoverflow.com/questions/4244896/dynamically-access-object-property-using-variable) – jonrsharpe Apr 15 '22 at 16:25
  • Thank you @jonrsharpe, it doesn't unfortunately. – JaeEmAre Apr 15 '22 at 16:33
  • 1
    What exactly do you mean _"not working"_? Give a [mre]. – jonrsharpe Apr 15 '22 at 16:34
  • This is for NextJS, and it appears that the OP is using CRA, but I believe this question is the more appropriate duplicate. https://stackoverflow.com/questions/64152943/cant-access-process-env-values-using-dynamic-keys – Brian Thompson Apr 15 '22 at 18:39
  • Webpack will evaluate environment variables at build time, so dynamic access isn't possible directly. If you inspect the JS where you have something like `const myvar = process.env.TESTVAR`, you would actually see `const myvar = 'value of test var';. – Brian Thompson Apr 15 '22 at 18:40

1 Answers1

0

Thank you everyone for your posts and help. We've decided to go a different route with this and will not be chasing this rabbit any longer.