Add environment variables in create-react-app project
11 March 2022 (Updated 11 March 2022)
Create .env.example
Create a .env.example
file that will serve as a template, providing defaults and placeholders. Check this file into Git. For example:
REACT_APP_API_URL=http://localhost:8080
REACT_APP_SENTRY_TOKEN=
Remember that create-react-app will only process env variables that begin with REACT_APP_
so make sure you include this prefix.
Create .env
Add .env
to your .gitignore
file. On local setup, each developer on your team can create a .env
file using .env.example
as a template.
REACT_APP_API_URL=http://localhost:8080
REACT_APP_SENTRY_TOKEN=abc123
Use env variables in JavaScript
console.log(process.env.REACT_APP_API_URL);
Use env variables in public/index.html
<title>%REACT_APP_SENTRY_TOKEN</title>
Tagged:
React tooling
Thanks for your comment 🙏. Once it's approved, it will appear here.
Leave a comment