How to obtain value from environment variable in React APP

Viewed 33

I have a React app that is using npm to be build.

What I am trying to do is to obtain the CURRENT_ENV value that is injected using a configMap in the AKS cluster.

I made a run.sh script that is run as ENTRYPOINT in the Dockerfile. In the run.sh I am exporting the CURRENT_ENV to a REACT_APP_CURRENT_VARIABLE. The run.sh file looks like this:

export REACT_APP_CURRENT_ENV=${CURRENT_ENV}

npx serve -s build -l 3000

At the pod level I am able to see the values from CURRENT_ENV and REACT_APP_CURRENT_VARIABLE, but at code level process.env.REACT_APP_CURRENT_ENVis undefined

I need to use the value from CURRENT_ENV env variable in the code to change a label dynamically based on the env on which the app is deployed.

1 Answers

you need to create .env file in your project directory. and add the environment variable to it.

Related