-1

I have my create-react-app with Express running perfectly fine on localhost but now that I want to publish it I need to move some stuff to environment variables obviously.

The thing is that I created my .env file in my /src folder (I can't move it to the root directory if I want to be able to access to it from my /src) and named variables with the REACT_APP_ prefix, like:

REACT_APP_USERNAME="TESTNAME"

I'm getting that .env file with

const myEnv = require('../.env')

Then in one of my components I try to get that variable but it´s not there... if I console.log(proccess.env) I can see the default values but nothing about my vars, what am I missing?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Pizzaboy
  • 331
  • 2
  • 14
  • 1
    You don't have to require the .env file, React will load the env variables for you. https://create-react-app.dev/docs/adding-custom-environment-variables/ – Junius L Jul 04 '23 at 07:27
  • Just did it @JuniusL, no errors but anyway I can´t see my variables... – Pizzaboy Jul 04 '23 at 07:31
  • Then give a [mre] _of that_, require-ing the file certainly isn't going to work. Note it's not the case that process.env is populated with the variables, as I explain in https://stackoverflow.com/a/76440819/3001761 – jonrsharpe Jul 04 '23 at 07:37

1 Answers1

0

Like Junius L said, "you don´t have to require the .env file" so after I deleted that require() I could move it to the proper root directory (instead of the /src directory) and problem fixed.

Thanks for your help.

Pizzaboy
  • 331
  • 2
  • 14