Join our Newsletter — 33% off our NHI Course
Home› Guides› Environment Variables and .env Files for Beginners: Where…
Guide Secrets Management

Environment Variables and .env Files for Beginners: Where App Secrets Hide and How They Leak

PDF for newsletter subscribers · free to read online

← All guides
By NHI Mgmt Group, under the editorial direction of Lalit Choda Updated 30 November 2026 10 min read
On this page

An environment variable is a named setting that a program reads when it starts, often holding passwords and keys.

This guide is for new developers, students and anyone who has seen a .env file and wondered what it is. You need no deep coding skills. It explains how these settings work and how to handle the secrets inside them safely.

In short

  • An environment variable keeps settings out of the code, so the same code can run in many places.
  • A .env file is a plain text file that loads these settings, and it often holds an app's secrets.
  • These secrets leak when the file is committed to Git, copied into images or printed in logs.
  • Keep .env files out of Git, and use a secrets manager for anything that matters.

What you will learn

  • Explain what environment variables and .env files are.
  • Describe why apps keep secrets in them.
  • Name the common ways these secrets leak.
  • Take simple steps to handle them safely.
Named setting (environment variable)
a name and a value that a program reads when it starts.
Settings file (.env file)
a plain text file listing those names and values.
Safe store (secrets manager)
a service that keeps passwords and keys locked up and hands them out when needed.

What are environment variables?

Think of a sticky note left on a desk for whoever works the next shift. The note says which door code to use today. Environment variables are like that note for a program.

The Twelve-Factor App method says settings that change between places, such as database details and passwords, should stay out of the code [1]. It recommends environment variables, because they change without touching code and are unlikely to be saved into Git by accident [1].

A .env file is a simple way to set them on your own computer. Each line holds one name and one value, written as KEY=value [2]. Many tools, including Node.js, can load the file when an app starts [2].

Where the comparison stops working: a sticky note is seen by one person. An environment variable can be read by other programs, logs and error reports, often without anyone noticing.

Why does it matter?

The keys in a .env file belong to the app, which is a non-human identity. Anyone who copies them can act as that app.

If you are a new developer

What could happen: you commit your .env file to Git, and the database password goes with it.

Check this: make sure .env is listed in your project's ignore file.

Next: Git Secret Scanning Guide

If you run a small app or website

What could happen: an error page or log prints every setting, including keys.

Check this: turn off debug mode in live systems and check what your logs record.

Next: Exposed .git and .env Files for Beginners

If you manage a team

What could happen: the same .env file is shared by chat, and nobody knows who has a copy.

Check this: ask where the team's real secrets live and who can read them.

Next: Secrets Management for Beginners

What does it look like?

Here is a made-up .env file for a small shop app. It shows the kind of values these files hold.

Example only. All values are made up.

# .env (EXAMPLE, do not commit)
APP_MODE=development                          # (1)
SHOP_NAME="Example Shop"                      # (2)
DB_URL=postgres://app:[email protected]/shop   # (3)
PAYMENT_API_KEY=EXAMPLE-0000-0000-0000        # (4)
DEBUG=true                                    # (5)
  1. A harmless setting that is different on your laptop and on the server.
  2. Another harmless setting. Not everything in the file is secret.
  3. In a real file, this line would contain the database user name and password.
  4. In a real file, this line would contain a payment service key that can move money.
  5. Debug mode. If it is left on for customers, error pages can print settings.

Why this is risky: secrets and harmless settings sit side by side in plain text, so the whole file gets copied around.

The safer way: keep the file out of Git, use separate keys for testing and live, and fetch live secrets from a secrets manager.

How does it work?

  1. The settings are written down. On a laptop, that is often a .env file. On a server, a hosting platform or other tool usually sets them.
  2. The app starts. A tool loads the file and adds each line to the app's environment. Node.js, for example, has an option called --env-file for this [2].
  3. The app reads the values. The code asks for a name, such as DB_URL, and gets the value back as text [2].
  4. The app uses the secret. It connects to the database or payment service with the key it read.
Diagram titled How app secrets travel, and where they leak. Four boxes in a row, linked by arrows: The .env file, plain text on a laptop or server; Loaded at start-up into the app's environment; The app reads each value as text; The app connects to the database or service. A note below lists common leak points: committed to Git, built into a container image, printed in logs or error pages, and shared by chat or email.
Figure 1. Secrets move from a file into the app, and can leak at several points on the way.

Leaks happen along this path. The OWASP Secrets Management Cheat Sheet warns that environment variables are often readable by other processes and may end up in logs or system dumps [3]. MITRE lists plain text secrets in environment variables as a known weakness in its official list [4].

OWASP's advice is to fetch secrets from a secrets manager where you can, and to treat environment variables as a fallback [3].

A worked example

This story is made up to show the idea.

Gideon Falk is a student who builds a small booking app for a local club. He keeps the database password and an email service key in a .env file.

He wants to share the project with a friend, so he pushes it to a public Git repository. He forgot to tell Git to ignore the .env file.

Pia Kowal, who helps run the club's IT, spots the file while reviewing the project. She tells Gideon straight away.

Gideon deletes the file and pushes again. Pia explains that this is not enough. The old version is still in the project history, and anyone may have copied it.

Together they cancel the email key and create a new one. They also change the database password. Then they check the email service's logs for anything sent that they did not send.

Next, Gideon adds .env to his ignore file. He also adds a .env.example file, with names but no values, so his friend knows which settings to fill in.

Gideon is relieved that they acted fast. Finally, Pia moves the live keys into the hosting platform's secret settings. The .env file on Gideon's laptop now holds only test values.

Myths and mistakes

What people thinkWhat is true
Environment variables are a safe place for secrets.They keep secrets out of code, but other programs, logs and dumps may still see them [3].
Adding .env to the ignore file removes it from Git.The ignore file only affects files Git is not already tracking [5]. A committed file stays in history.
Deleting a leaked .env file fixes the problem.Copies may already exist. Replace every secret that was in it.
The same keys can be used on a laptop and the live site.Use separate keys, so a leak from a laptop does not open the live system.
A .env file is a kind of secrets manager.It is a plain text file. A secrets manager controls who can read each secret and keeps a record.

What can I do this week?

  • Check that .env is in the ignore file of every project you work on.
  • Search your Git history for any .env file that was committed in the past.
  • Add a .env.example file with names only, no values.
  • Make sure debug mode is off in every live system.

Checklist

  • I can explain what an environment variable and a .env file are.
  • Our .env files are listed in the Git ignore file.
  • No .env file sits in our Git history.
  • We use different keys for testing and for the live site.
  • Live secrets come from a secrets manager or the platform's secret settings.
  • Our logs and error pages do not print secrets.
  • We replace any secret that leaks, not just delete the file.

Common questions

Should I commit my .env file?

No. Add it to your Git ignore file before your first commit. Share a .env.example file instead, listing the setting names with no real values. Each person, or the hosting platform, then fills in their own values.

What is the difference between a .env file and an environment variable?

An environment variable is the setting itself, held by the running program. A .env file is just one way to write those settings down. A tool reads the file and turns each line into an environment variable when the app starts.

I pushed my .env file by mistake. What now?

Treat every secret in it as exposed. Cancel each key or password and create new ones. Then check the services' logs for misuse. Removing the file from Git history helps, but only after the secrets are replaced.

Are environment variables safe on a server?

They are better than secrets written in code, but not ideal. Other programs, logs or crash reports may see them. For important secrets, a secrets manager is safer, because it controls who can read each one.

What should go in a .env file?

Settings that change between your laptop and other places, such as a database address or test keys. Keep live, high-value keys out of it where you can. Use test keys that can do little harm if they leak.

Where next?

Ready for the detail? Read the practitioner guide: Environment Variables and .env Files. It covers leak paths in containers, CI and servers, and safer ways to deliver secrets.

Also useful:

Related NHI Mgmt Group resources: Secrets Management Guide, Git Secret Scanning Guide and How to Remove Secrets From Git History.

Key terms

Environment variable
A value given to a program when it runs, so settings are not written into the code.
.env file
A local text file that stores environment variables for an app, often including keys and passwords.
Secrets manager
A system that stores, rotates and delivers secrets such as API keys, passwords and tokens.
.gitignore
A file that tells Git which files to leave untracked. It does not protect files already committed.
Hardcoded secret
A credential written directly into code, scripts or configuration files.
Non-human identity
A software identity, such as an app, service or AI agent, that uses its own credentials to reach other systems.

Sources and further reading

  1. Adam Wiggins, "III. Config", The Twelve-Factor App. https://12factor.net/config (accessed 30 Nov 2026).
  2. Node.js, "Environment Variables", Node.js documentation. https://nodejs.org/api/environment_variables.html (accessed 30 Nov 2026).
  3. OWASP, "Secrets Management Cheat Sheet", OWASP Cheat Sheet Series. https://cheatsheetseries.owasp.org/cheatsheets/Secrets_Management_Cheat_Sheet.html (accessed 30 Nov 2026).
  4. MITRE, "CWE-526: Cleartext Storage of Sensitive Information in an Environment Variable". https://cwe.mitre.org/data/definitions/526.html (accessed 30 Nov 2026).
  5. Git, "gitignore: Specifies intentionally untracked files to ignore", Git documentation. https://git-scm.com/docs/gitignore (accessed 30 Nov 2026).

PDF for subscribers

Take this guide with you

Every section, table and checklist in one PDF, with links to the sources and related guides. Subscribe to our weekly NHI & AI Identity Journal and we’ll email you a download link.

Free weekly newsletter

Subscribe to the NHI & AI Identity Journal

The latest on NHI and Agentic AI security – articles, research, breaches, news and events every week.

Bonus 33% off our NHI Course when you subscribe.

PDF Downloads for Newsletter Subscribers

Get the guide

Enter your details and we’ll email you a download link.

The full guide is free to read online. The PDF is a thank you for subscribing.

Fill in all fields marked * and tick both boxes to continue.