What are Environment Variables ?

These are the variables that play an important role in the configuration of any application/program. They are just like normal variables, a key-value pair. Also, they contain critical pieces of information.

So Why to use Environment Variables ?

As said earlier, they help in configuration, their values differ from platform to platform, and according to the Application Condition/ use cases. And their values are set outside the program, typically through functionality build into the operating system or microservice.

Different Application Environments.

  • Development
    • This is the environment that’s on the developer computer. Here all the development related stuff are done like coding/ bug fixing/modification/features enhancement, etc, etc
  • Testing
    • In this environment application behavior and actions are monitored and a comparison between expected output (result) and actual output (result) is checked. Here the bugs and failures are checked.
  • Staging
    • This stage is similar to the production environment. Your codes reside on a server this time instead of the local machine. It will connect all the services similar to the production. All the hardcore testing happens here like new database migrations, new features, configuration changes. This stage acts as a demo stage for the clients or the friendly users (recall alpha, beta release)
  • Production
    • It is the final stage of any software product where the user interact with the software/application. All the ongoing business information related to the user resides here and the revenue of the company is generated from this stage only.

Now comming to the point, How to use environment variables ?

Lets consider the example of the NodeJS. And try to learn the things.

To use Environment Variables, it’s super easy. You can use .env file

Environment Variable in NodeJS

And then use dotenv module to get Environment Variables.

Can I get Environment Variables directly ?

Yes, you can, If you are using LINUX/MAC then use the following command VAR_NAME="VAR_VALUE" in the terminal. For Windows PC use $env:VAR_NAME="VAR_VALUE".

Linux / MAC Environment Variable Set
Windows Powershell

Setting Environment Variable in Linux System.

Setting Environment VAriable in Windows System.


Variable Naming and values

Variable Naming and values are up to you. You can use same variable for the different environment or use different variable name.

But, My recommendation will be to use the same variable name in all the stages as DB_HOST="XYZ" where the variable name DB_HOST should be the same in development, testing, staging, and production. Because your codes will probably run on different machines for different stages.

Does and Don’ts for Environmant Variables

  • Don’t save these variables within your code block
  • Do not commit your environment variables to version controls like git.
  • Do not directly share them.
  • Save them outside your code/program
  • Save them on OS level
  • Use Encryption and Decryption on Environment Variables
  • Create .env_example file and put dummy data just to elaborate on other developers that this application needs a given set of environment variables.
  • Always use exception handling with Environment Variables.

If you have feedback or questions about this page, add a comment below or send message to me on LinkedIn https://www.linkedin.com/in/adeshkumarsingh/

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.