-1

Simple question,

I have the following environment variables:

REACT_APP_API_URL=http://localhost:1234

Although this evaluates to undefined?

console.log(process.env.REACT_APP_API_URL)

Using create-react-app, from their documentation this is all I need to do? .env file is in the root directory, react even re-compiled when I changed it.

user231071
  • 119
  • 7

4 Answers4

0

I believe you have to include your environment variables in global process.env. For this you can use some sort of package like dotenv. This blog link would be helpful. https://trekinbami.medium.com/using-environment-variables-in-react-6b0a99d83cf5

rao yasir
  • 49
  • 4
  • No need for dotenv in create-react-app. I don't think this is anything to do with what I am doing, and this article is over 3 years old... – user231071 Sep 06 '21 at 17:05
  • Please add further details to expand on your answer, such as working code or documentation citations. – Community Sep 06 '21 at 18:46
0

Make sure you have the .env file in your root folder (same as where package.json is) and not in /src. Then double check that you have restarted the server (kill the npm start and start again). If it's not that, you will have to share more information, because it doesn't seem to be anything wrong in the few bits of information you have shared.

dglozano
  • 6,369
  • 2
  • 19
  • 38
0

Can you provide some screenshots? It seems to work fine for me. Please see the below screenshots.

Console Log

Directory Structure

Browser output

Ashad Nasim
  • 2,511
  • 21
  • 37
rao yasir
  • 49
  • 4
0

inside your app can you try to console.log it like this?

const {REACT_APP_API_URL} = process.env;

console.log(REACT_APP_API_URL)

Grabbed from here: https://adostes.medium.com/using-environment-variables-in-a-react-application-ac3b6c307373

Dan
  • 361
  • 1
  • 5
  • 17