You don’t need dotenv (and I doubt that library will work at runtime in a client-side application anyway).
create-react-app actually provides this functionality out of the box, assuming you’re using react-scripts@0.5.0 or higher.
The steps are as follows:
- Create a
.envfile in the root of your project. - Add a variable, starting with the prefix
REACT_APP_. - Access it via
process.env.
That second bit is the important part – to avoid you exposing variables by accident, create-react-app forces you to use a prefix as a way of saying “yes, I know what I’m doing”.
If you’re not intending to push the file to source control (which you shouldn’t be, if it’s got secret keys in!), then using an .env.local file is more idiomatic – that requires react-scripts@1.0.0 or higher, though.