📊 We use cookies & analytics to improve your experience. Learn more
Latest

Full-Stack Web Development Bootcamp Module 4 · Lesson 10 of 15

0% Exit to course

Module 4 · Ship it: deploy, monitor, hand over Lesson 10 of 15

Environment variables and secrets

Learning objective

Database passwords and API keys live in the environment, never in the repository. A leaked key is the most common self-taught project failure.

The concept

An environment variable is a value the app reads at run time instead of having it written into the code: the database password, the API key, the port. Your app asks the operating system for process.env.DATABASE_URL and gets a different answer on your laptop and on the server, with no code change.

Keep the names in a committed .env.example with placeholder values, and the real values in .env, which goes in .gitignore — before the first commit, not after. If a key ever reaches a public repository, treat it as leaked and rotate it; deleting the commit does not un-leak what has been cloned.

Common mistakes

The mistake is committing .env in the first five minutes because the build would not start otherwise. Spend that time on .gitignore instead. Environment variables are also why the same build works locally and in production, so this is setup that pays for itself all the way through.

Worked example

process.env.DATABASE_URL with a .env file locally and never committed — add .env to .gitignore before the first commit, not after.

Your practice task

Move every secret in your project into environment variables and confirm .env is ignored by git.

Do this before moving on. Reading a solution teaches the shape of it; typing it teaches you.

Key takeaway

An environment variable is a value the app reads at run time instead of having it written into the code: the database password, the API key, the port.

Create free account or sign in

Almost there

15 lessons left of 15

Finish every lesson to unlock the certificate. Each one ends in a practice task — do them and the final project brief writes itself.

Next recommended course Data Analytics with SQL & Excel