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.
@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.

@dev-prosper
Copy link

Tried this example but it throws an error saying "process is not defined"

I created my React App using Vite

@AmitKulkarni23
Copy link

Hello, I am getting an undefined while trying to use/access the environment variable from .env file
Here is what I have verified and checked:

  • The .env file is in the root folder (the same level as package.json)
  • I have named the variable in the .env as REACT_APP_API_GATEWAY_PREFIX
  • I have restarted the development server after adding the environment variable
  • NodeJS version - v20.17.0
  • "react": "18.2.0",
  • "react-native": "0.73.3"
  • I am trying to access the variable using process.env.REACT_APP_API_GATEWAY_PREFIX

This is for a React Native project. Can someone help me out here?

@Pear104
Copy link

Pear104 commented Oct 4, 2024

for someone who still get undefined: terminate and rerun your project again

@phoebesu1025
Copy link

For the people who use Vite react app, you can try this:

in your .env file, you will start your variable name with VITE_something_something_something=value
image

In your react file, the part you want to get the variable from .env, you write import.meta.env.VITE_something_something_something
image

@aasiflm10
Copy link

for those who are not able to access env variables and process.env.APIKEY shows undefined. You need to know this :
if your project has a similar directory structure :
project
->client(This is your frontend)
->components
->SomeComponent.tsx ( and you need to access env variable in this file then .env should be in client directory not the main project directory)

I made a seperate .env for client and it solved my issue. I think it happens becuase you run "npm start" in client directory ( which is your frontend ) and it will only bundle files from client only.

Hope this helps.

@swapnilahmedshishir
Copy link

swapnilahmedshishir commented Oct 28, 2024

if come to this error "process is not defined" . apply this i solve this problem
create your project root folder in ,env file and use this
VITE_OPEN_API_KEY=keyValue
when use you .env key
const apiKey = import.meta.env.VITE_OPEN_API_KEY;

if not solve in up method alt last apply this in .eslintrc.cjs file
env: {
node: true,
commonjs: true,
browser: true,
es6: true,
},

@Chachu21
Copy link

thanks a lot you save me for losing a lot of time

@OluwatobilobaGp
Copy link

Thank you so much.

@TusharKumar0907
Copy link

if come to this error "process is not defined" . apply this i solve this problem create your project root folder in ,env file and use this VITE_OPEN_API_KEY=keyValue when use you .env key const apiKey = import.meta.env.VITE_OPEN_API_KEY;

if not solve in up method alt last apply this in .eslintrc.cjs file env: { node: true, commonjs: true, browser: true, es6: true, },

It worked for me Thanks !

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