0

So I have a file named .env with the following contents

NODE_PATH=./src
NODE_ENV=what
TEST=test

And I am calling that in my index.js in my react app.

 require("dotenv").config();
console.log(process.env);

...

shows the following output

NODE_ENV: "development"
PUBLIC_URL: ""

I thought maybe I declared another .env file somewhere else, but thats not the case. I searched my project for the PUBLIC_URL and it's not located anywhere in my project. I don't even know what else to check at this point.

Snoopy
  • 1,257
  • 2
  • 19
  • 32

3 Answers3

1

In react code you have to compile the environment variables in at, well, compile time because at run-time there it is only possible to access a fake process.env object. Unless you are using server side rendering.

See also: Passing environment-dependent variables in webpack

Catalyst
  • 3,143
  • 16
  • 20
0

If you're using CRA then you'll need to do: REACT_APP_TEST=test and reload the dev server to have it show up as expected in your app.

Colin Ricardo
  • 16,488
  • 11
  • 47
  • 80
0

If you have used create-react-app for bootstrap your project then you have to use environment variables like REACT_APP_NODE_ENV=development.

After adding any new environment variable, you have to restart the development server.

aditya81070
  • 678
  • 1
  • 5
  • 22