logdown.js icon indicating copy to clipboard operation
logdown.js copied to clipboard

Environment variables inside React App

Open ngmiduc opened this issue 4 years ago • 2 comments

Hi,

is there a way to include the NODE_DEBUG environment inside a React App? As React filters all env variables our that aren't prefixed with REACT_APP_<name>.

best Minh

ngmiduc avatar Dec 07 '20 09:12 ngmiduc

Hello, @ngmiduc

If you'll provide some *_DEBUG env variable for you react app during build, this will hardcode that value in your code and it won't be possible to change it later at runtime. Do you really want this?

In any case, you can pass REACT_APP_DEBUG during build and then somewhere before running your app just mirror this value in localstorage:

window.localStorage.setItem('debug', process.env.REACT_APP_DEBUG)

SleepWalker avatar Dec 09 '20 06:12 SleepWalker

Hi

I didn't mean to hard code it into build. I want to run it in development and I think the env variable NODEDEBUG gets filtered out by the react app since it only allows prefixed eng variables to be accessible in the app.

So when I initialize the logdown in some file in the app, it can not access the env Variable to check if the console logs are enabled or disabled.

I created now a separated file that imports logdown and sets the state by a react env variable. Do enable and disable the logs .

ngmiduc avatar Dec 09 '20 11:12 ngmiduc