DEV Community

loading...
Cover image for react-envs

react-envs

garronej profile image Garrone Joseph ・1 min read

https://github.com/garronej/react-envs

Create react app provides no official way to inject environment variable from the server into the page.

When you run yarn build create react app does bundle all the variables prefixed by REACT_APP_
and expose them under process.env (see here).

The problem, however, is that you likely don't want to build your app on the server.

You want to be able to do something like:

docker run --env FOO="xyz" org/my-app

and get access to FOO in your TS/JS code like process.env["FOO"].

The CRA team suggests to introduce placeholders in the public/index.html
and do the substitution on the server before serving the app. This solution involves a lot of hard to maintain scripting.

react-envs abstract away the burden of managing environment variable injection as well as providing a type-safe way
to access them in your code.

image

image

Discussion (0)

pic
Editor guide