score:0

so when I was looking at passing environment variables to React today, I happened across this website https://create-react-app.dev/docs/adding-custom-environment-variables/ which says that all enviroment variables used by React have to start with REACT_APP for security reasons.

score:1

I tried a lot of things, of which none seemed to work. What I did as a workaround for this is make an API in Express which will be called by the front-end and will forward the env vars. Something of the sort:

app.get('/getEnvironmentVars', (_, res) => {
    res.json({ ENVIRONMENT: process.env })
})

Then you just fetch it in the front-end code like:

export async function getEnvironmentVarsFromExpress() {
    return await fetch('/getEnvironmentVars').then((res) => res.json())
}

Related Query

More Query from same tag