Skip to content

Instantly share code, notes, and snippets.

@Haugen
Created November 9, 2018 17:33
Show Gist options
  • Save Haugen/f6d685f18b4bd8a3cf5bcf6272577c5b to your computer and use it in GitHub Desktop.
Save Haugen/f6d685f18b4bd8a3cf5bcf6272577c5b to your computer and use it in GitHub Desktop.
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.
@Qaisrajpoot12
Copy link

thanks

@ahmed-esmail-1
Copy link

That was Amzing ! Thx

@ahmed-esmail-1
Copy link

what happens at deployment I am using git pages, how will I let the built know if I am not uploading?

Go to the repo settings, then pages, there you can choose from what branch to deploy it

@abouzeyd
Copy link

i can't access to my environment variable in react application
const BASE_URL = process.env.REACT_APP_BASE_URL;

@codeangel352
Copy link

I will appreciate if you provide a fit example.

@kevinknights29
Copy link

Hi all, if it is not working for you, try formatting the variables like this: REACT_APP_API_KEY = "YOUR_API_KEY". Then test it out withconst apiKey = process.env.REACT_APP_API_KEY;

@thatcatcancode
Copy link

the key/value syntax is fine without quotes and spaces. you just need to restart your dev build with npm run start so webpack can repackage your env var into a new build

@shashank0092
Copy link

it is showing undefined

@minhajul-im
Copy link

it has given me an error:

process is not defined

@godcodevo
Copy link

Hello, minhajul-im.
Can you give me your screenshort?

@AakashNarwade
Copy link

How to add multiple keys in .env ? how can these multiple keys separated in .env file in react app?

@godcodevo
Copy link

godcodevo commented Feb 12, 2024 via email

@mediacrisis
Copy link

If you are getting undefined as a response, double check that your .env file is in the root folder of your app and not a subdirectory.

@AbhishekAnan00
Copy link

i used modified env process like as import.meta.env.VITE_REACT_APP_API_KEY and also store it with variable but it didnt work . suck this time with env .

@roshan-jha-23
Copy link

nice
image
image
such as this one.

@nameernihad
Copy link

That was nice 😍👍

@zafar122
Copy link

zafar122 commented Apr 1, 2024

That was nice

@godcodevo
Copy link

This is nice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment