2. Environment & Config
Understand environment variables and runtime configuration.
Why configuration matters
Every environment (local development, testing, production) needs different credentials and URLs. Environment variables let us change behavior without editing source code.
Environment variable basics
- Environment variables are key/value pairs that live outside of your code. Example:
MONGODB_URI=mongodb://localhost:27017/tasks. - NestJS reads them through
process.env. We’ll pass these values intoMongooseModule.forRootso the app knows which MongoDB to connect to. - Never hardcode secrets (passwords, API keys) inside
.tsfiles. Store them in environment variables instead.
Using .env files
.envfiles are plain text files you load in development to set environment variables automatically.- We add a non-secret template named
.env.exampleso new developers know which variables they must define. - Local files such as
.env.development.localstay out of version control and contain your personal values. - In production, we use AWS Parameter Store / Secrets Manager (see Configuration & Environment).
Local vs. cloud MongoDB
- Run MongoDB locally via Docker for quick feedback. You will create
docker-compose.dev.ymlin this chapter. - Use MongoDB Atlas when you can’t run Docker or want a hosted option. You’ll paste the Atlas connection string into the
.envfiles created here.
After reviewing these ideas, continue to Prepare MongoDB to stand up Docker or Atlas and capture your .env settings, then move on to Connect MongoDB for the NestJS wiring.
Version control: branch for Chapter 2
Run the following commands before making changes so your work lives on a dedicated branch that can become a pull request:
git checkout main
git pull origin main
git checkout -b feature/environment-configCommit as you go; you’ll push this branch and open a PR after finishing the chapter.