'not able to get .env variable value in react js
when i tried to get the value from .env file, it says undefined, i have defined variable in .env API_URL, is there anything which i am missing in react js ? i am using dotenv npm, can anyone please help me how to resolve this issue ? here i have uploaded my code, any help will be really appreciated
const dotenv = require('dotenv');
const fs = require('fs');
alert(process.env.API_URL);
.env
API_URL=https://********/api/v1/
Solution 1:[1]
The two important things to remember when defining Environment Variables in your react project
- Always reload your application after making a change to the .env file
- Always prefix your variables with REACT_APP_
Solution 2:[2]
Check whether the .env file is in the root folder of working directory
As per the react documentation : You must create custom environment variables beginning with REACT_APP_. Any other variables except NODE_ENV will be ignored to avoid accidentally exposing a private key on the machine that could have the same name. Changing any environment variables will require you to restart the development server if it is running.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Himanshu Singh |
| Solution 2 |
