12

I have an issue. In my Host/cloud solution, I must use environment variables of pricing for each country this way 'defined in their "Environment Variables").

BASIC_PRICE_FR_PRODUCT = "50";
COMPLEX_PRICE_FR_PRODUCT = 100;

BASIC_PRICE_UK_PRODUCT = "37";
COMPLEX_PRICE_UK_PRODUCT = "200";

BASIC_PRICE_ES_PRODUCT = "75";
COMPLEX_PRICE_ES_PRODUCT = "300";

I can access those using process.env.XXX such as process.env.BASIC_PRICE_FR

As you see these environment variables depend on the country as the price vary from one country to the other.

In our node.js app, the challenge is that when a function is executed, it is self aware of the country so, we can (and must) use the "current" country and the current country_iso_code ("fr" for example), and with this we must use the pricing that match this country.

After reading on SO some posts on "dynamic variable names" , I tried eval, global[] and window[] like below, but none work and all outputs "undefined" values

//note: iso_code_3166_we_can_use is something passed to the function by the final user or by some other lambda in the function context.
const current_country_iso_code_uppercase = iso_code_3166_we_can_use;
const basicPrice   = parseInt( "process.env.BASIC_PRICE_" + current_country_iso_code_uppercase + "_PRODUCT")
console.log(basicPrice)//bug here as outputs "undefined"

EDIT

The suggestion of using process.env['xxx'] did not work so I add here the results

console.log(process.env.BASIC_PRICE_FR_PRODUCT);//outputs 50
console.log('BASIC_PRICE_' + iso_code_uppercase + '_PRODUCT' );//just to be sure :): outputs BASIC_PRICE_FR_PRODUCT
console.log( process.env['BASIC_PRICE_' + iso_code_uppercase + '_PRODUCT'] );// DOES NOT WORK, outputs undefined
Mathieu
  • 4,587
  • 11
  • 57
  • 112
  • Have you tried `process.env["BASIC_PRICE_" + current_country_iso_code_uppercase + "_PRODUCT"]`? – Sebastian Simon Jan 14 '19 at 22:31
  • @Xufox i'll try now, no i haven't, i tried some circumvented/complex stuff ...why didn't i think of it? will update if it works – Mathieu Jan 14 '19 at 22:33
  • @Xufox did not work, see my edits – Mathieu Jan 14 '19 at 23:14
  • Does it work if you group all six variables in a single object, e.g. `PRODUCT_PRICES`, then use `process.env.PRODUCT_PRICES["BASIC_PRICE_" + current_country_iso_code_uppercase + "_PRODUCT"]`? Apparently `process.env` is a bit… [“special”](https://github.com/webpack/webpack/issues/6091). – Sebastian Simon Jan 14 '19 at 23:20
  • @Xufox, i'll try to find another solution as this was given as example, i might have 200 to group and very hard to maintain on each new country/prices. I wanted to reach "dynamically" the environment variables set. – Mathieu Jan 14 '19 at 23:23
  • @Xufox but i think you're right, the issue must be on webpack process.env and bug with statically xpressions, i added a "webpack" tag on my question – Mathieu Jan 14 '19 at 23:29
  • This is not a duplicate as it seems to be an issue specific to dynamically accessing `process.env` variables, as noted by @Xufox – dkershaw Mar 06 '19 at 20:02
  • 1
    UPDATE: looks like there's a webpack fix in progress: https://github.com/webpack/webpack/pull/8721 – dkershaw Mar 06 '19 at 20:17
  • `process.env['BASIC_PRICE_' + iso_code_uppercase + '_PRODUCT']` should work on the server, so you might want to fetch it. – thpo Nov 11 '21 at 18:58

2 Answers2

22

Use [] to dynamically access an object's property:

var country = 'FR'
var price   = process.env['BASIC_PRICE_' + country + '_PRODUCT']
Marco
  • 7,007
  • 2
  • 19
  • 49
-3
//Create an empty dictionary,
var process_env_dict = {};
process_env_dict['env'] = process.env;

//then you can access it with the below statement as you expected

var result = JSON.parse(JSON.stringify(process_env_dict))['env']['BASIC_PRICE_' + country + '_PRODUCT'];