.env & dotenv (Node.js)
A .env file stores environment variables as KEY=value lines for local development. The dotenv package loads them into process.env at startup, so your code reads config the same way it would in production.
Node.js
Setting many environment variables by hand each time you run your app is tedious. A .env file holds them as simple KEY=value lines, and calling require('dotenv').config() early in your program reads the file and populates process.env, so process.env.DATABASE_URL works locally just as it does in production.
The .env file must be git-ignored — it typically contains secrets like API keys and database passwords. A common convention is to commit a .env.example with the keys but no real values, documenting what variables the app needs without exposing them.
Load dotenv before any code that reads those variables (at the very top of your entry file), or the variables will be undefined when other modules initialize. In production you usually do not use a .env file at all; the platform injects real environment variables directly, and recent Node versions can load .env files natively with --env-file.
Syntax
require("dotenv").config(); // loads .env into process.envExamples
JavaScript
// .env (git-ignored)
// PORT=4000
// JWT_SECRET=supersecret
// top of server.js
require("dotenv").config();
console.log(process.env.PORT); // 4000
console.log(process.env.JWT_SECRET); // supersecret