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