Menu

1 / 2

Environment Variables Explained (with .env file)

DEV Community·Sohana Akbar·7 months ago
#SbCYwLe2
Long article (273 words) — no AI summary yet.
Reading 0:00
15s threshold

Ever hardcoded an API key in your code? We've all been there. Then came the panic commit removing it right after. Let me show you the right way.

What are environment variables?

Environment variables are dynamic key-value pairs stored outside your application code. They live in the shell session or system environment, making them perfect for configuration that changes between environments.

Think of them as settings you can change without touching your codebase.

Why use them?

Security - Keep secrets out of version control

Portability - Same code, different configs (dev/staging/prod)

Convenience - No more config files inside your repo

The .env file

A .env file is a plain text file in your project root that lists environment variables:

bash

.env

PORT=3000

DATABASE_URL=postgresql://localhost:myapp

API_KEY=abc123secret

How to use it

Most programming languages have packages to load .env files:

Node.js (using dotenv):

javascript

require('dotenv').config()

const port = process.env.PORT

const…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.