Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
December 22, 2021 05:36 am GMT

Use environment variables in create-react-app projects

cra-envs is a module that help you safely bundle server environment variables into statically served React SPA.

Motivation

We want to be able to do docker run --env FOO="xyz" my-org/my-create-react-app
then access FOO in the app like process.env["FOO"].

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.

For this use case 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.

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


Original Link: https://dev.to/garronej/use-environment-variables-in-create-react-app-projects-78

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To