I am trying to set some environment variables (for making API calls to dev/prod endpoints, keys depending on dev/prod, etc.) and I'm wondering if using dotenv will work.
I've installed dotenv, and I am using webpack.
My webpack entry is main.js
, so in this file I've put require('dotenv').config()
Then, in my webpack config, I've put this:
new webpack.EnvironmentPlugin([ 'NODE_ENV', '__DEV_BASE_URL__' //base url for dev api endpoints ])
However, it is still undefined. How can I do this correctly?
Dotenv is wonderful. It allows you to use environment variables in your code, hence separating the code from its running environment.
Dotenv is commonly used (create-react-app uses it, so there) and will get the variables from our . env file and add them to the global process.
Sorry for picking up old question, but
react-scripts actually uses dotenv library under the hood.
With [email protected] and higher, you can work with environment variables this way:
.env
REACT_APP_BASE_URL=http://localhost:3000
App.js
const BASE_URL = process.env.REACT_APP_BASE_URL;
See docs for more details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With