I want to add a .env file in my React application. The project wasn't created using Create React APP. And without Webpack. I'm struggling to find a proper solution for this
For a easy solution you could try to use react-app-env.
You could folllow this simple guide. In brief:
install it:
yarn add --dev react-app-env (or npm install --save-dev react-app-env)
change the start and build scripts:
"scripts": {
"start": "react-app-env start'",
"build": "react-app-env build'",
"test": "react-app-env test'",
...
}
finally add at least two configuration files (on project root folder): development.env
and production.env
. By default development.env
is used imported on npm start
and npm test
, and production.env
on npm build
.