The Power Of the dotEnv (Environment File)

Yaman Omar Alashqar
3 min readAug 13, 2022

--

Just like you have different clothes for different occasions and different environments, applications often run in different environments where each environment operates with different values and different flavor's.

These different environments could be separated such as the testing, development and production environments, or maybe it could be separated to serve different clients using the same product.

What kind of information is stored in the .env files?

Examples on information that could change from one environment to another: the API and the port number, the username and password for the database, the credentials for the mail server, certain flags to disable some authentication functions during development, the directory to store uploaded files or the path to any static files and folders, cron jobs schedule time, and endless variables which are all independent of the code.

Why should we use the .env files?

Environment variables are helpful in software development, let me explain why.

First Advantage

Imagine that this information is embedded as constants or variables within the code, what will happen if you wanted to change the mail’s server password?

Simply if this information was baked with the code, you will need to rebuild and redeploy the whole project, and I am sure that NOBODY is going to be happy to redeploy the whole application just because the password of the mail server has changed.

With the magic of the .env (dotenv) file we don’t have to rebuild or change the code anymore when we want to support a new environment.

Second Advantage

Another great advantage is that some credentials are not supposed to be shared with the development team, or if you are sharing the source code with the world.

Again with the magic of the .env (dotenv) file your code can be shared and viewed by anyone without having to share the credentials with the development team or the world.

How does a .env file look like?

Its just a simple text file. You can specify key value pairs and you can write comments using the #. Here is an example how a .env file looks like:

.env file example

Conclusion

Environment variables are useful in all projects, simply they provide security by hiding the credentials and sensitive data from the code and other developers, and it provides flexibly where you can modify certain flags and values without the need to rebuild and redeploy the project.

Thanks for reading …

My LinkedIn Account:

CONTACT ME FOR ANY HELP/SUGGESTIONS (OR EVEN TO SAY HI 😎)

https://www.linkedin.com/in/yaman97/

--

--

No responses yet