Environment variables may look boringājust key-value pairsābut they are one of the most important parts of modern software development. They keep your apps secure, flexible, and portable.
In this blog, weāll explore:
- What env variables are (in plain English).
- Why they are so important.
- A simple
.envsetup with an example. - Beginner tips (3 golden rules).
- Best practices for teams.
- Using env variables with Docker.
- Using env variables in CI/CD pipelines.
By the end, youāll understand why they are the way they are and how to use them like a pro. š
š What Are Environment Variables?
An environment variable is like a secret note you keep outside your code. Your app can read it at runtime, but it doesnāt live inside the source code.
Example:
DB_USER=admin DB_PASSWORD=secret123 API_KEY=abcd1234 š Instead of writing sensitive info directly in code, you store it here and access it when needed.
š¤ Why Are They Important?
Environment variables exist to solve three big problems:
- Security ā No one wants passwords and API keys pushed to GitHub.
- Flexibility ā The same code should work in development, testing, and productionāwithout rewriting.
- Portability ā Anyone running your app can configure it differently without changing the source.
Thatās why they are the way they are: they separate configuration from code.
ā” A Simple .env Setup
- Create a file called
.envin your project:
DB_USER=admin DB_PASSWORD=secret123 PORT=4000 - Install the dotenv package (for Node.js projects):
npm install dotenv - Use it in your code:
require('dotenv').config(); const dbUser = process.env.DB_USER; const dbPassword = process.env.DB_PASSWORD; console.log(`Connecting with ${dbUser}:${dbPassword}`); Now your secrets are outside the code, but still accessible when your app runs. ā
š¼ļø Example Scenario: Database Connection
Without env variables:
const dbUser = "admin"; const dbPassword = "secret123"; ā Hardcoding = insecure + inflexible.
With env variables:
const dbUser = process.env.DB_USER; const dbPassword = process.env.DB_PASSWORD; ā Secrets hidden, and you can change them per environment.
š” 3 Golden Tips for Beginners
Never commit
.envfiles
Add.envto your.gitignore. Secrets should never end up on GitHub.Use defaults for local development
const port = process.env.PORT || 3000; If no env variable is set, your app still runs on port 3000.
- Use separate
.envfiles
.env.development.env.production.env.test
Keeps environments clean and avoids costly mistakes.
šØāš©āš§āš¦ Best Practices for Teams
When working with others, managing env variables becomes tricky. Hereās how to do it right:
- ā
Share a
.env.examplefile ā This contains variable names, but not the real secrets.
DB_USER= DB_PASSWORD= API_KEY= ā Use secret managers for real apps ā Tools like AWS Secrets Manager, Vault, or Doppler are safer than
.envfiles.ā Document everything ā Make sure teammates know which env variables are required.
š³ Using Env Variables with Docker
Docker makes it easy to pass env variables into containers.
- Define them in a
.envfile:
DB_USER=admin DB_PASSWORD=secret123 - Reference them in
docker-compose.yml:
version: '3' services: app: image: my-app env_file: - .env - Inside your container, your app can read
process.env.DB_USERas usual.
š This keeps your container flexibleāno need to rebuild when you change configs.
āļø Using Env Variables in CI/CD Pipelines
In CI/CD (like GitHub Actions, GitLab CI, or Jenkins), environment variables are the standard way to pass secrets securely.
GitHub Actions example:
name: Deploy App on: push jobs: build: runs-on: ubuntu-latest steps: - name: Checkout Code uses: actions/checkout@v3 - name: Run App run: node app.js env: DB_USER: ${{ secrets.DB_USER }} DB_PASSWORD: ${{ secrets.DB_PASSWORD }} š The real secrets are stored in GitHubās Secrets Manager, not in your repo.
šÆ Final Thoughts
Environment variables might seem small, but they are the glue that makes modern apps secure and adaptable.
- They keep secrets out of your code.
- They let the same app run in different environments with ease.
- They make apps portable across laptops, servers, Docker, and CI/CD pipelines.
Whether youāre building a personal project or deploying to production, always remember:
š Donāt hardcode it. Env it. š±
Top comments (0)