Unable to get a system env variable inside react

3/11/2020

I have a minikube setup where I have an API written in SprintBoot APP and have a separate REACT APP for UI.

In the deployment YAML file for REACT, I am setting one of the env variables that are available from a config map. However, when I do process.env.VARIABLE, it gives me undefined. I am pretty sure I am using it the wrong way and hence want to know if there any way to access a system environment variable inside the react code.

Sample code in REACT (NOT using my variable. Rather just a PATH variable):

onClickFunction() {
        console.log("Clicked!");
        alert('Button clicked! Env val = '+String(process.env.PATH));
    }

In the alert, I see undefined. But when I did a bash into the pod, I see this:

$ kubectl exec -it react-js-567dd44ccc-2kdbz -nem bash
root@react-js-567dd44ccc-2kdbz:/app# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

root@react-js-567dd44ccc-2kdbz:/app#

The configmap is giving me the spring boot service name so that I can use it to call my API from UI. I am just trying to access that variable name here. Is this the right way to do this? Or should I be building this in an entirely different way?

-- Arun Subramanian
configmap
environment-variables
kubernetes
reactjs

1 Answer

3/11/2020

May be, you have require exact path to .env file.

Try this...

const result = require('dotenv').config({ path: '/full/custom/path/to/your/env/vars' })

if (result.error) {
  throw result.error
}
-- Sumit Kumar
Source: StackOverflow