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 |
abc@xyz.com |
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
Next.js Environment Variables
In this article, we are going to see how to use environment variables in Next.js. Environment variables in Next.js are a way to set configuration values that are used by your application. They can be used to store data such as the name of your company, the port your application will run on, or any o
2 min read
What are the different scopes of variables in PHP ?
Variable Scopes: The scope of a variable is defined as its extent in the program within which it can be accessed, i.e. the scope of a variable is the portion of the program within which it is visible or can be accessed. Depending on the scopes, PHP has three variable scopes. Local variables: The var
3 min read
Environment Variables are Undefined in Next.js App
Environment variables play a crucial role in web development, especially in configuring sensitive information such as API keys, database URLs, and other configuration settings. If your environment variables are showing up as undefined in your Next.js app, it can disrupt functionality. This issue typ
3 min read
How to Use Environment Variables in Vite?
In order to create your Vite application in different environments such as development, staging and production, you need to use environment variables. They enable you to manage confidential data like API keys or service URLs without having to code them inside your source code. By using environment v
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.
6 min read
Reading Environment Variables From Node.js
Environment Variable: The two fundamental concepts of any programming language are variables and constants. As we know that constants and variables both represent the unique memory locations that contain data the program uses in its calculations. The variable which exists outside your code is a part
2 min read
How to Handle Different Environments in a Next.js?
Environment variables in NextJS are fundamental settings or values that can change based on the application's deployment environment. They are crucial for separating sensitive information, such as API keys, database credentials, or configuration details, from the application codebase. In a NextJS pr
4 min read
Setting up environment variables in Node.js in a platform independent way
Environment variables are a good way to ensure security of your precious API keys and secret stuff as well as adaptive code. However, setting up environment variables might be a bit tedious when working on various machines having different Operating Systems. At such times, a portable and project-spe
3 min read