# .env Files and package.json

> Two files that most projects have. How a .env file keeps secrets out of the code, and how package.json lists what a JavaScript project needs.

**Course:** [JSON, YAML and File Formats](https://learn.benibix.com/learn/development-formats)  
**Time:** 12 min

**After this lesson you can:**

- Keep a secret in a .env file
- Stop a .env file from being committed
- Read the parts of a package.json

## Where a project keeps its secrets and its list of needs

A .env file holds secret settings as simple lines, one name and one value each. A program loads each line as an environment variable when it starts. Since version 20.6, Node.js can do this on its own. package.json is the main settings file for a JavaScript project. It is written in JSON.

An environment variable is a named value that the system hands to a program as it starts. The code reads the name and never holds the secret itself. So the same code can run on your laptop with test values and on the live server with real ones.

Each machine has its own .env, with its own values. The file must never go into Git. List it in .gitignore on the first day. Many projects commit a file called .env.example instead, with every name and no real values, so a new teammate knows what to fill in.

Some tools read a .env file for you. In Node.js, the --env-file flag does it. In Python, a small package called python-dotenv does it. Either way, the values arrive as environment variables, and the code reads them by name.

package.json sits in the top folder of a JavaScript project. It gives the project a name and a version. It lists the packages the code needs to run. A second list holds the ones you only need while you build and test.

It also holds scripts, which are short names for longer commands, like dev or test. Running npm install reads package.json and fetches every package on its lists. Running npm run dev runs the command saved under dev.

The worst mistake is committing a .env file. Deleting it later does not help, because Git keeps the old version in its history. Treat any secret that was ever committed as leaked, and swap it for a new one.

```ini
DATABASE_URL=postgres://localhost:5432/shop
PAYMENT_KEY=test_123
```
A .env file: one name and one value per line. This file stays off Git.

```json
{
  "name": "my-site",
  "version": "1.0.0",
  "scripts": {
    "dev": "next dev"
  },
  "dependencies": {
    "react": "^19.0.0"
  }
}
```
A small package.json with a name, a version, one script and one package.

![A box called package.json holds four parts: the name my-site, the version 1.0.0, a dev script set to next dev, and a dependency on react. An arrow marked npm run dev leads to a box that runs next dev. An arrow marked npm install leads to a node_modules folder that holds react and every package it needs.](https://learn.benibix.com/diagrams/dev2-package-json.svg)

### Example: Jack commits his .env file

Jack pushes his project to a public repo, .env and all, with a live payment key inside. Within 2 hours, a bot that scans public code finds the key.

He makes a new key, turns the old one off, and adds .env to .gitignore. He also adds a .env.example with the 3 names and no values.

**Words in this lesson:** [.env File](https://learn.benibix.com/term/development/env-file), [Environment Variable](https://learn.benibix.com/term/infrastructure/environment-variable), [package.json](https://learn.benibix.com/term/development/package-json)

**Question:** I committed a .env file and then deleted it. Is the secret safe now?

**Answer:** No, Git keeps it in history, so swap the secret for a new one. Git keeps each old copy of each file. Anyone with the history can read it. So the only fix is a new secret.

---

Source: https://learn.benibix.com/learn/development-formats/env-files-and-package-json
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-19
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
