Luiz E. Luiz E. - 1 year ago 96
React JSX Question

read environment vars in a simple react app

I create an app with

module, and in a component, I'm trying to read an env. var, like this

<p style={textStyle}>Environment: { process.env.AWS_ENV || process.env.NODE_ENV }</p>

I start the app with
AWS_ENV=live npm start
and it never reads this var. the result is always
. I noticed tho, if I build the app with
npm run build
, the output will be
, but anyway I cannot read the AWS_ENV var

Answer Source

create-react-app using To load ENV into Webpack and to pass it in browser, run this commands in console:

echo -e 'REACT_APP_AWS_ENV=live' > .env
npm start

Note REACT_APP_ prefix special to create-react-app.

Then in your components:

{ process.env.REACT_APP_AWS_ENV }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download