.env Files and package.json
Level 3 · Config and docs files
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 contentsLesson 6 of 11
Level 1 · Files and formats
Level 2 · Data formats
Level 3 · Config and docs files
Level 4 · Characters and escapes
Level 5 · IDs, dates and patterns
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
12 mincovers 3 glossary entries
Worth reading first: Config Files, YAML and TOML
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.
DATABASE_URL=postgres://localhost:5432/shop
PAYMENT_KEY=test_123{
"name": "my-site",
"version": "1.0.0",
"scripts": {
"dev": "next dev"
},
"dependencies": {
"react": "^19.0.0"
}
}Worked example
Jack commits his .env file
The words for this lesson3
Check yourself
I committed a .env file and then deleted it. Is the secret safe now?
Finished this lesson?
Marking it complete also marks its 3 glossary entries learned, which updates your roadmap and your saved page. 0 are already marked.