avatarWaqqas Jabbar

Summary

The website content discusses the benefits and implementation of using a single .env file for both frontend and backend applications within a monorepo setup, specifically for a Django backend and React.js frontend.

Abstract

The author of the website content describes their experience developing a backend API server with Django and a frontend with React.js within a single monorepo. This approach allows for the use of a shared .env file to configure environment variables for both applications, which simplifies deployment and reduces the likelihood of errors. The .env file contains settings specific to each application as well as common configurations. The author explains how to pass the .env file to Docker containers using docker-compose.yml and how to access the environment variables within both the React.js and Django applications. For React.js, the author suggests a method to make environment variables more accessible within the JavaScript code, while in Django, the python-decouple package is recommended for reading the .env file. The conclusion emphasizes that using a single .env file adheres to the DRY (Don't Repeat Yourself) principle, promoting a more efficient and maintainable software development process.

Opinions

  • The author advocates for the use of a monorepo to house both frontend and backend applications, citing the advantage of sharing a single .env file.
  • It is implied that having a single .env file reduces the complexity of managing environment variables and makes the deployment process less error-prone.
  • The author expresses a preference for using environment variables over hard-coded configurations for better flexibility and security.
  • The use of Docker and docker-compose.yml to manage environment variables is presented as a standard and effective approach.
  • The author suggests that accessing environment variables directly in the application code can be unsightly and recommends a more JavaScript-friendly method for React.js applications.
  • The python-decouple package is recommended for Django applications to easily read from the .env file, indicating the author's endorsement of this tool.
  • The article concludes by reinforcing the DRY principle as an accepted software development practice, suggesting that the single .env file strategy aligns with this principle.
  • The author promotes an AI service, ZAI.chat, as a cost-effective alternative to ChatGPT Plus (GPT-4), indicating a belief in the value and performance of this service.

Single .env file for frontend and backend apps

Recently, I was developing a backend API server in Django and frontend in React.js. Instead of having different Git repos for both application, I went for a Monorepo. The FE app was in a subdirectory within the repo.

One advantage of having a single repo is that it allows one to share a single .env file between the FE and BE apps. FE would need the URL of the server to call the APIs and BE would need the FE URL to configure its CORS configuration. Having them in a single .env file, makes deployment less error prone.

The .env file

FE and BE development frameworks can be configured by environment variables. This is a great way to pass different configurations to the apps, instead of hard-coding them. Docker also supports setting environment variables, through a “.env” file, which are read by the FE and BE apps.

A typical .env file would look something like the one below. It has some configuration that are specific to FE and BE apps and some that are common to both. The file would most probably contain other variables too.

Passing .env file to docker

One can configure .env via docker-compose.yml. A typical section for FE app would look something like below.

The important parameter is env_file. All the variables defined in the .env file would be set when the docker container starts. You can also pass .env file in command line while running docker command, like:

docker compose --env-file .env.dev up

Reading .env in React.js App

All the environment variables that start with “REACT_APP” keyword is available through “process.env” variable with Node.js app.

Using the variable directly within the app is an eye-sore. In order to make it more JS-friendly, I did the following:

Thus instead of using something like process.env.REACT_APP_API_SERVER, I can use config.apiServer.

Reading .env file in Django App

In order to ready the same .env file in Django app, one can use “python-decouple” package.

pip install python-decouple

In settings.py, we can do the following

from decouple import config
SITE_URL = config("REACT_APP_API_SERVER")
CORS_ALLOWED_ORIGINS = [ config("REACT_APP_SERVER_URL"), ]

Conclusion

Using single .env file making our configuration more DRY, which is one of the accepted software development principles.

Django
React
Monorepo
Software Development
Recommended from ReadMedium