Skip to content

Instantly share code, notes, and snippets.

View Timonwa's full-sized avatar
👻
World Class Dev

Timonwa Akintokun Timonwa

👻
World Class Dev
View GitHub Profile
@Timonwa
Timonwa / env-file-in-react-create-app.md
Created November 6, 2022 10:35 — forked from Haugen/env-file-in-react-create-app.md
Using .env file environment variables with create-react-app project

For a simple way to use environment variables in your react-create-app project, follow these steps:

  1. Create a new file named .env in the root of your project.
  2. In your new .env file, add a new key=value pair. For security reasons, you must prepend your key with REACT_APP, for example REACT_APP_API_KEY=abcdefg123456789
  3. Restart your server development server. In order for React to find and register your newly created environment variable you must restart your server. Do this every time you add or change a variable.
  4. Your new variables will now be available throughout your React app via the global process.env object. In our case, we could get our API key using process.env.REACT_APP_API_KEY.

Additinal notes

  • Since we commonly store secrets in .env you probably want to add it to .gitignore.
  • You don't need to install the dotenv package or anything else for this to work.