Skip to content
Learn Netverks

Lesson

Step 23/36 64% through track

env-config

Environment configuration

Last reviewed May 28, 2026 Content v20260528
Track mode
nodejs_server
Means
Node sandbox
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Environment configuration: the syntax, APIs, and habits you need before advancing in Node.js.

Teams ship Environment configuration on every Node.js codebase—skipping it leaves gaps in debugging and code reviews.

You will apply Environment configuration in contexts like: REST/GraphQL APIs, BFF layers, CLIs, webhooks, and real-time services (with WebSockets).

Run JavaScript on the Node runner when configured—never mix arbitrary shell commands in lessons.

When you can explain the previous lesson's ideas without copying starter code.

Twelve-factor apps store config in the environment—ports, database URLs, API keys—not hard-coded in source. Node reads them via process.env.

Patterns

const port = Number(process.env.PORT ?? 3000);
const dbUrl = process.env.DATABASE_URL;
if (!dbUrl) throw new Error('DATABASE_URL required');

.env files (local dev)

dotenv loads .env into process.env during development—never commit secrets. Production injects vars via platform (Docker, Kubernetes, Vercel, Railway).

Validation at startup

Fail fast if required vars missing—better than cryptic errors on first DB call. Libraries like Zod or envalid schema-check env shapes.

Important interview questions and answers

  1. Q: NODE_ENV values?
    A: Conventionally development, test, production—frameworks toggle caching and logging verbosity.
  2. Q: Secrets in git?
    A: Never—use secret managers, rotate on leak, scan repos with gitleaks.

Self-check

  1. Why validate env at startup?
  2. Should .env be committed?

Tip: Validate env vars at startup with a schema (e.g. Zod)—fail fast if DATABASE_URL is missing instead of crashing mid-request.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • dotenv prod caution?
  • PORT env standard?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump