Reputation: 1638
I created .env
file in the root of my directory with data REACT_APP_API_URL=http:\\localhost:3001
, but when I run and get value from process.env
, I get an empty object, how can I fix that?
Upvotes: 2
Views: 1381
Reputation: 5054
You need to integrate variable with start script.
"scripts": {
"start": "REACT_APP_API_URL=http://localhost:3001 react-scripts start", //like this
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
}
then you need to run your app. it will take all paramerters.
Or another way to run it pass it using command line. Ex: you need to pass env variable first before loading scripts
REACT_APP_API_URL=http://localhost:3001 npm start
You can also use package for better env management(cross-env) It will take care all env related management. Feel free to check there doc.
If you want to use cross-env you have to only do this for running enviornment from .env file(Note:.env file should be on package.json level)
"scripts": {
"start": "cross-env react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
it will load all your configuration from .env file whenever you want to use.
Upvotes: 1