'environment variable undefined in react js
I am trying to get the environment variable from .env file, I have followed all steps but don't know why it's showing an undefined value
.env file
REACT_APP_NAME=user
in React
console.log(process.env.REACT_APP_NAME);
Solution 1:[1]
Ensure that the donenv package is installed (
npm install dotenv)Ensure the .env file is in the root directory
Make sure the following code is present in your main file:
import dotenv from "dotenv";
dotenv.config();
The import statement in the above code may need to be adjusted based on what stack you're using. I'm assuming this is node, since you haven't provided any details. Comment and let me know if this doesn't work.
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 | Sam Spade |
