Backend Basics

Environment Variables and Configuration in Backend Apps

By MinorNode Editorial Team · · 4 min read

Every backend application depends on settings that change from one place to another: which database to connect to, which port to listen on, how verbose the logs should be. Hard-coding those values into your source files works for a weekend project and falls apart the moment a second environment appears.

Environment variables are the most common answer. They are simple, supported everywhere and understood by almost every hosting platform. Used carefully, they keep your code identical across machines while the behavior adapts to its surroundings.

What Environment Variables Are

An environment variable is a named string that the operating system hands to a process when it starts. Your program reads the value by name. On a Linux or macOS shell you might set one like this.

export PORT=8080
export LOG_LEVEL=debug

Inside a Python program, os.environ["PORT"] returns the string 8080. In Node.js the same value lives at process.env.PORT. Notice that the value is always text. If you need a number or a true or false flag, your code must convert it.

Why Separate Configuration From Code

The idea behind this approach is old and sound: code stays the same, configuration changes. There are a few good reasons for that.

  • You can run the same build in development, testing and production without editing files.
  • Secrets such as passwords and tokens stay out of your repository history.
  • Operations staff can change a setting without touching or rebuilding the application.
  • Containers and cloud platforms inject variables natively, so no extra tooling is needed.

A useful test is this: could you publish your repository publicly today without leaking anything sensitive? If not, some configuration is living in the wrong place.

Using a Local .env File

Typing a dozen export commands every morning is tedious. Many projects keep a plain text file named .env in the project folder with one NAME=value pair per line, and a small library loads it when the program starts.

PORT=3000
DATABASE_URL=postgres://localhost/myapp_dev
LOG_LEVEL=debug

Two rules matter here. First, add .env to your .gitignore so it is never committed. Second, commit a safe template, often called .env.example, which lists every variable with a placeholder value. New teammates copy it, fill in their own values and get running quickly.

A .env file is a convenience for local development. In production, set the variables through your hosting platform, your container orchestrator or your process manager instead of shipping a file.

Defaults, Required Values and Validation

Not every setting deserves the same treatment. Sort them into three groups.

  1. Safe defaults: things like a port number or a log level, where a sensible fallback exists.
  2. Required values: a database address or a signing key, where guessing would be dangerous.
  3. Optional features: flags that turn behavior on or off.

For required values, fail fast. Check everything at startup and exit with a clear message if something is missing or malformed. A server that crashes immediately with "DATABASE_URL is not set" is far easier to fix than one that starts happily and fails on the first request an hour later.

A tidy pattern is to read all variables in a single configuration module, convert and validate them there, and export a plain object. The rest of the codebase imports that object and never touches the environment directly. This also makes testing easier, since you can build a configuration object by hand.

Handling Secrets Carefully

Environment variables are convenient for secrets, but they are not a vault. Keep these points in mind.

  • Never print the full environment in logs or error pages.
  • Do not pass secrets as command line arguments, since process listings can reveal them.
  • Rotate credentials when a person leaves a project or when you suspect exposure.
  • Give each environment its own credentials so a leak in testing cannot touch production.
  • For high-stakes systems, consider a dedicated secrets manager that your app queries at startup.

If a secret ever lands in a commit, deleting the line is not enough. Treat it as compromised, replace it and then clean the history if needed.

Separating Environments

Most teams run at least three environments: development on a laptop, a staging area that mimics production, and production itself. Avoid scattering checks like if (env === "production") through your code. Prefer to express each difference as its own setting, such as a flag for verbose errors or a URL for an external service. Code that depends on specific variables stays easier to reason about than code that depends on an environment name.

Common Mistakes

  • Reading process.env or os.environ all over the codebase, which makes missing values hard to trace.
  • Forgetting that every value is a string, so the text "false" is treated as true in a careless condition.
  • Committing a real .env file once and assuming a later delete fixes the problem.
  • Using different variable names in different services for the same concept.
  • Documenting nothing, leaving new developers to guess which variables exist.

A short section in your README listing each variable, what it does and whether it is required prevents most of these problems.

Frequently Asked Questions

Are environment variables secure enough for passwords?

They are a reasonable middle ground for many apps, because they keep secrets out of source control. They are readable by anything running as the same user and can leak through logs or crash dumps, so applications with strict security needs often add a dedicated secrets manager.

Should I commit my .env file?

No. Commit only a template such as .env.example containing placeholder values. The real .env file belongs in .gitignore, because it usually holds credentials that should never enter version control.

How do I handle numbers and booleans from environment variables?

Remember that every value arrives as a string, so convert it explicitly in one configuration module. Parse numbers with a numeric function and compare booleans against specific strings such as true, rather than relying on truthiness.

Related reading