How to access environment variables from Kubernetes pod in React/Node.js
P粉790819727
2023-09-05 09:03:09
<p>We are defining some environment variables in kubernetes pod and when I try to use them in node or React FE code using process.env.TEST (because TEST exists in env as secret), I always get undefined, But when I see there are variables on the pod. </p>
<p>Is there any other way to access these variables or do we need to do something explicitly on Node.js or React.js. </p>
Environment variables in a Kubernetes Pod can be accessed in Node.js using
process.env.
, similar to how they are accessed in any Node.js application. You're doing it the right way, so if the value is undefined, something might not be set correctly.React Environment Variables: If you are trying to use environment variables in your React application, you need to prefix them with
REACT_APP_
. Only environment variables starting with this prefix will be embedded in the build. Therefore, you will useprocess.env.REACT_APP_
to access them in your code.