Using dotenv gives undefined
See original GitHub issueDescription After setting up .env file and installed dotenv still cant find variables and get undefined.
Expected behavior
- add .env file with config in root
- npm install --save dotenv
- import dotenv using require(‘dotenv’).config(); or ES6 syntax in index.js file
- use process.env.REACT_APP_REST_API_LOCATION to set an state in my react app
Actual behavior
using the above step i get undefined both in my console.log aswell in the fetches were i want to use it.
i got my envVars.env file with the line
REACT_APP_REST_API_LOCATION=http://localhost:8080
and import the dotenv config in my App.js as
require('dotenv').config();
i have tried setting the require in most places in my application with same result.
Enviroment npm: 6.0.1 react-scripts: 1.1.4 dotenv: 5.0.1 node: 6.10.3 OS: Windows 10
Packages (wanted => installed) react: ^16.2.0 => 16.3.2 react-dom: ^16.2.0 => 16.3.2 react-scripts: ^1.1.4 => 1.1.4
Issue Analytics
- State:
- Created 5 years ago
- Comments:6
Top Results From Across the Web
Environment variables undefined using dotenv - Stack Overflow
Recently, I decided to hide the keys into an .env file using the dotenv package. This is what my file looks like :...
Read more >Why my env variables are undefined in other files except app ...
Hypothesis: You are using dotenv.config() after importing certain files. Therefore env variables are not taken into account in said files.
Read more >.env variables “undefined”.. Ever stuck in a scenario ... - Medium
Ever stuck in a scenario when you're building an application whether it is a REACT or NEXT based but don't know how to...
Read more >Can someone explain me why .env variables are undefined ...
In my javascript file. require("dotenv").config(). console.log(process.env.MYVAR). In my .env file. MYVAR=A. logs undefined. I just can't make sense of REPL.
Read more >why dotenv process.env undefined Code Example
Rename your file to just .env, removing any prefix require it as early as possible in your application with the code snippet below ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
If you have installed dotenv remove it from your package.json. create a
.env.development
(fileName starts with “.” ) add propertyREACT_APP_REST_API_LOCATION=http://localhost:8080
try to access it usingprocess.env.REACT_APP_REST_API_LOCATION
Wanted to add that you might have to run npm build because “It works by actually replacing strings at build-time, not run-time. So each build of your application will have the values hard-coded into it.” and also add REACT_APP_ to the front of any variables.