'ReactJS Environment variables are undefined on development

I cannot use my environment variables on my development (localhost) with ReactJS.

I made sure of following:

  1. My .env file is in the root directory
  2. My env variable has a prefix of REACT_APP_* and the usage is process.env.REACT_APP_*
  3. My env variable does not contain '' and contains = as well as it does not contain ; or , so the definition is REACT_APP_SOMETHING = something
  4. Restarted server multiple times by ctrl + c and yarn start
  5. I checked (and copied & pasted) all the variable names to make sure and avoid misspelling

Even after all of this if I try to use them (for example with console.log(process.env.REACT_APP_SOMETHING)) it prints only undefined. Is there anything that has to be done in package.json scripts? The definition is only:

"scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },

I used create-react-app to initialize my project.

Printing for example console.log(process.env.NODE_ENV) works fine. I tried even creating .env.development file with no luck.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source