What are environment variables in development ?
Last Updated :
31 Dec, 2020
Environment variables is a widely used term, but what does it actually mean and why does it even exist ?
Technical Definition: An environment variable is a dynamically set value that is used throughout program and determines properties of a program on each device. They are part of the environment in which a program executes.
For example, a program can get the values for a specific key from environment variables, say Proxy to go through a proxy tunnel while making network requests if proxy variable exists.
Benefits of environment variables are -
- Security
- Easy maintenance and adaptive code
Let's understand it with the help of another example. Imagine, you get a paid service like a paid weather API service and got a secret token for authentication which you included in your weather project.
For example, Environment variables in JavaScript can be like this:
const weather_api_key = "YourVeryUsefulAndSecretToken";
Now, you made a public repository for this project like most of us do. But wait, this means anybody can use your key in their projects while you're paying for it. That's a big issue !
So, should you stop keeping your project in public repositories?
No ! You should always try to add your projects to Github or similar VCS services to enhance your portfolio. We just need a way to isolate this secret key from plain code, yet usable in the code. The answer is Environment variables.
Environment variables are a set of key-value pairs setup on the local setup or hosting service, rather than direct inclusion in plain code, which are usable in your code,
For example,
weather_key | YourVeryUsefulAndSecretToken |
database_key | SecretDatabaseKey |
support_email | [email protected] |
We can include these keys in the code to use their respective values in our code. For example, in JavaScript:
const weather_api_key = process.env.weather_key;
Where the weather_key refers to the actual token value i.e. "YourVeryUsefulAndSecretToken"
Edit and add .env or other files with environment variables in the .gitignore file, so that it remains untracked by git. You can push this code to your repositories without any security concerns now !
Environment variables solved the security issue, but it has another benefits too. Environment variables ensure Adaptive code.
For example, if the code uses the above mentioned environment variable named support_email wherever it has to display support email on the website. Later you wish to change it due to some reasons. In such cases, you just need to change it in environment variables configuration. Had it been as a plain code, It would be a tedious task to change it everywhere.
Setting up environment variables differs based on your Operating System and Hosting services, so get help from your service-specific docs or guides. Environment variables are generally scoped globally across the Operating System, but there are some ways to set up environment variables specific for your project too. Checkout this article on setting up environment variables in Node JS.
Similar Reads
Environment Variables in ElectronJS ElectronJS is an Open Source Framework used for building Cross-Platform native desktop applications using web technologies such as HTML, CSS, and JavaScript which are capable of running on Windows, macOS, and Linux operating systems. It combines the Chromium engine and NodeJS into a Single Runtime.
7 min read
Next.js Environment Variables Environment variables are a fundamental aspect of modern web development, allowing developers to configure applications based on the environment they are running in (development, testing, production, etc.). In Next.js, environment variables provide a flexible and secure way to manage configuration s
3 min read
Manage Staging Environments in ElectronJS ElectronJS is an Open Source Framework used for building Cross-Platform native desktop applications using web technologies such as HTML, CSS, and JavaScript which are capable of running on Windows, macOS, and Linux operating systems. It combines the Chromium engine and NodeJS into a Single Runtime.I
6 min read
What is the difference between Host objects and Native objects ? In this article, we will learn about what are Host objects and Native objects, and their differences. JavaScript objects are broadly classified into 2 categories - native javascript objects and host javascript objects. Native objects: Native javascript objects are standard javascript objects which a
2 min read
JavaScript Global Variables Global variables in JavaScript are variables that can be accessed and modified from anywhere in the code, regardless of the scope where they were declared. These variables are stored in the global object (window in browsers and global in Node.js). The declaration of global variables and accessing th
2 min read
ES6 | Variables A variable is a named container in the memory, that stores the values. In simple words, we can say that a variable is a container for values in Javascript. The ES6 Variable names are called identifiers. The rules to keep in mind while naming an identifier. An identifier can contain alphabets and num
4 min read
Environment Variables in Linux/Unix Environment variables, often referred to as ENVs, are dynamic values that wield significant influence over the behavior of programs and processes in the Linux operating system. These variables serve as a means to convey essential information to software and shape how they interact with the environme
6 min read
Environment Variables in Java In Java, Environment variables are widely used by operating systems to deliver configuration data to applications. Environment variables are key/value pairs with both the key and the value being strings, similar to properties in the Java platform. What are Environment Variables in Java?In Java, Envi
5 min read
C# Environment Class SetEnvironmentVariable() Method With Examples Environment Class provides information about the current platform and manipulates, the current platform. It is useful for getting and setting various operating system-related information. We can use it in such a way that retrieves command-line arguments information, exit codes information, environme
3 min read
Environment Variables in Python Environment variables are key-value pairs that live in our system's environment. Python reads some of these variables at startup to determine how to behave, for example, where to look for modules or whether to enter interactive mode.If thereâs ever a conflict between an environment variable and a co
3 min read