question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using dotenv gives undefined

See original GitHub issue

Description 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

image

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:closed
  • Created 5 years ago
  • Comments:6

github_iconTop GitHub Comments

12reactions
sanjeshcommented, May 18, 2018

If you have installed dotenv remove it from your package.json. create a .env.development (fileName starts with “.” ) add property REACT_APP_REST_API_LOCATION=http://localhost:8080 try to access it using process.env.REACT_APP_REST_API_LOCATION

1reaction
GeorgeBelangercommented, Nov 30, 2018

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found