Skip to content
How-to

What is a .env file? A beginner's guide

A .env file is a plain text file of KEY=value lines, kept next to your code, that holds the settings an application needs to run: database addresses, API keys, ports and feature switches. A library or the runtime reads it at startup and turns each line into an environment variable, so the code can read process.env.DATABASE_URL or os.environ["DATABASE_URL"] instead of hard-coding the value. Because it usually contains secrets, a .env file should never be committed to git.

Published 6 min read By the ProductEnv team

What does a .env file look like?

It is the simplest configuration format there is. Each line holds one setting: a name, an equals sign and a value. Lines starting with # are comments, and blank lines are ignored. The file is literally named .env, with the leading dot that hides it in most file browsers, and it usually sits in the root folder of the project.

.env
# Where the app listens
PORT=3000

# Services it talks to
DATABASE_URL=postgres://app:secret@localhost:5432/shop
REDIS_URL=redis://localhost:6379

# Third-party keys (test mode)
STRIPE_SECRET_KEY=sk_test_51Hx0000000000000000
MAIL_FROM="Shop Team <hello@example.com>"

# Feature switches
ENABLE_NEW_CHECKOUT=false

Names are conventionally upper case with underscores. Values are plain strings: even 3000 and false arrive in your code as text, so the code converts them when it needs a number or a boolean. Quotes are needed when a value contains spaces or a #, and the exact quoting rules vary a little between the libraries that read the file.

Why do apps keep configuration in environment variables?

The same code runs in several places: your laptop, a teammate's laptop, a CI pipeline, a staging server and production. Each place needs different values. Your laptop talks to a local database, production talks to the real one, and the payment key in development must be a test key. Environment variables let one build of the code pick up whatever values the place it runs in provides.

  • Secrets stay out of the source code, so reading the repository does not reveal the production database password.
  • Changing a setting needs no code change, only a new value and a restart.
  • Every hosting platform, container runtime and CI service knows how to set environment variables, so the approach works everywhere.

On servers, the hosting platform usually sets these variables directly. The .env file is mostly a convenience for local development: instead of exporting twenty variables in your shell every morning, you write them in a file once and let the app load it.

How does an application load a .env file?

A .env file is not special to the operating system. Something has to read it and copy its lines into the process environment, and that something depends on your stack:

How common stacks load a .env file
StackHow the file is loadedHow code reads a value
Node.js 20.6 or laternode --env-file=.env app.jsprocess.env.PORT
Node.js with the dotenv packagerequire("dotenv").config() at startupprocess.env.PORT
Pythonload_dotenv() from the python-dotenv packageos.environ["PORT"] or os.getenv("PORT")
Next.js, Vite and similar frameworksAutomatically, from .env and mode-specific filesFramework rules decide what reaches the browser
Docker ComposeA .env file next to the Compose file fills ${VAR} in it; env_file: passes values to a containerWhatever the containerized app uses

Most loaders don't overwrite a variable that is already set in the environment, which is useful: a value set by your hosting platform or typed in the shell wins over the file. Check the documentation of your loader, such as the Node.js command-line options, when the order matters.

app.py
import os
from dotenv import load_dotenv

load_dotenv()  # reads .env from the current folder; existing variables win

port = int(os.getenv("PORT", "8000"))
database_url = os.environ["DATABASE_URL"]  # fails loudly if missing

Values in the browser are public

Frontend frameworks copy some variables into the JavaScript bundle at build time, usually those with a prefix such as NEXT_PUBLIC_ or VITE_. Anything that reaches the browser can be read by every visitor, so never give a secret one of those prefixes.

Should a .env file be committed to git?

No. A .env file normally holds passwords and API keys, and git keeps every committed version forever, in every clone and fork. Add it to .gitignore before the first commit:

.gitignore
# Local configuration with secrets
.env
.env.*
# ...but keep the template
!.env.example

If the file is already tracked, git rm --cached .env stops tracking it without deleting your copy. That does not remove it from older commits, so treat every value that was ever committed as exposed and rotate it. The .env file best practices guide covers what to do after a leak.

What is a .env.example file?

Since the real .env is not in the repository, new developers need another way to know which variables exist. That is the job of .env.example: the same keys, with placeholders or safe defaults instead of real values, committed alongside the code.

.env.example
PORT=3000
# Postgres connection string
DATABASE_URL=postgres://user:password@localhost:5432/shop
REDIS_URL=redis://localhost:6379
# Get a test key from the payment provider dashboard
STRIPE_SECRET_KEY=
MAIL_FROM="Shop Team <hello@example.com>"
ENABLE_NEW_CHECKOUT=false

A newcomer copies it to .env and fills in the blanks. The weak spot is that someone has to remember to update the example whenever a key is added, and then still has to hand over the real values somehow.

How do teams share .env values safely?

The tempting answer is to paste the file in a chat. It works once and causes trouble for months: copies that can't be revoked, versions that drift apart, and no record of who has what. The safer pattern is to share access instead of files. Keep the values in one encrypted place, give each person a role, and let them fetch the current values with a command.

That is what a secrets manager such as ProductEnv does. You import your .env once, invite teammates, and each of them runs npx productenv pull to write their own .env with the current values. It can also generate the .env.example for you, so the template never falls behind. Getting started with ProductEnv walks through the setup, and the guide to sharing .env files compares the other options.

Frequently asked questions

Is a .env file secure?

Not on its own. It is plain text, readable by anyone with access to the folder, and it often ends up in backups, chats or git history by accident. Keep it out of git, restrict who can read it on shared machines, and keep the source of truth in a secrets manager.

Where should a .env file be placed?

Usually in the root folder of the project, next to package.json, pyproject.toml or the Compose file. Most loaders look in the current working directory, so run the app from that folder or pass the file's path explicitly. Monorepos often keep one .env per app folder.

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

Environment variables are a feature of the operating system: every process has a set of named strings it can read. A .env file is just a convenient place to write some of them down. A loader reads the file and copies its lines into the process environment when the app starts.

Does a .env file work on Windows?

Yes. The file is read by your application's loader, not by the operating system, so it behaves the same on Windows, macOS and Linux. Save it with plain line endings and no byte order mark to avoid an invisible character at the start of the first key.

Can a .env file contain comments?

Yes. A line that starts with # is a comment in every common loader. Comments after a value on the same line are handled differently between libraries, so put a comment on its own line, or quote the value if it contains a #.