ReactJS

React environment variables not working or undefined

In ReactJS environment variables are fetched from .env files. If you set the variable in .env file and your variables returned undefined check the below items.

Assumption: You have used Create React App (CRA) to bootstrap your application

  1. The .env file should be in the root for you application folder. That is one level above your src folder, the same place where you have your package.json
  2. The variable should be prefixed with REACT_APP_
  3. You need to restart the server to reflect the changes in your code.
  4. You should access the variable in your code like this
    process.env.REACT_APP_SOME_VARIABLE
  5. No need to wrap your variable value in single or double quotes.
  6. Do not put semicolon ; or comma , at the end of each line.

Happy coding ☕

Computer engineer interested in programming, electronics, application architecture, data visualization, automation and performance optimization.

5 Comments

Leave a Reply