If you used
create-react-app then some of the magic below would have already been done for you.
npm install dotenv --save
- Next add the following line to your app.
- Then create a .env file at the root directory of your application and add the variables to it. Variables needs to start with
REACT_APP_for it to work.
REACT_APP_API_KEY = 'my-secret-api-key'
- To access
This did work for a project that was created with.
Instead I had to access the env data as
const url = process.env.REACT_APP_API_URL;
- Finally, add .env to your .gitignore file so that Git ignores it and it never ends up on GitHub.