ProgramGuru

Environment Variables

Environment Variables — why it matters

Environment variables provide configuration passed to a Node process by its parent or deployment environment. Their values are strings or undefined, so parse and validate numbers and required settings before using them. Secrets in environment variables still need safe deployment practices and must not be printed into logs.

What you’ll learn

  • Explain environment variables and the problem it solves.
  • Identify the relevant compile-time checks and runtime responsibilities.
  • Run the example, predict its output, and try a realistic modification.

Understand the mechanism

parsePort applies a default only if the input is missing. It then checks both integer shape and allowed TCP-port range. process.env.PATH is read safely without printing its value.

Using process.env.PORT as number with a TypeScript assertion does not convert its string value.

Follow the concept flow

Environment Variables — concept flowenv value string | undefined → Parse and validate → Use typed configEnvironment Variables1env value string |undefined2Parse and validate3Use typed configBrowser code needs browser APIs; Node code needs Node.js APIs.
Follow the responsibilities from input to checked result.

Run the primary TypeScript example

Save as environment-variables.ts. The following program passed strict TypeScript compilation, and the displayed output was captured by executing its emitted JavaScript with Node.js 22.16.0.

TypeScript source

import { env } from "node:process";
function parsePort(raw: string | undefined): number {
  const port = Number(raw ?? "3000");
  if (!Number.isInteger(port) || port < 1 || port > 65535) throw new Error("Invalid port");
  return port;
}
console.log(parsePort("8080"));
console.log(parsePort(undefined));
console.log(typeof env.PATH === "string");

Verified output

8080
3000
true

Compile: npx tsc --strict --target es2022 --module commonjs environment-variables.ts; run: node environment-variables.js. For Node.js API examples, install @types/node first. For browser projects, use the browser-specific instructions below.

The example runs in Node.js 22.16.0; if it reads runtime environment details, your displayed values may differ.

Trace the result

parsePort applies a default only if the input is missing. It then checks both integer shape and allowed TCP-port range. process.env.PATH is read safely without printing its value.

Predict what changes if you alter one input. Then run the code to compare your reasoning with the real output.

Common mistake and correction

Using process.env.PORT as number with a TypeScript assertion does not convert its string value.

Identify whether the failure is a type-checking issue, a configuration issue, or a runtime behavior issue before choosing a fix.

Practice the concept

Read APP_MODE and accept only development or production; reject all other strings with a helpful message.

Write down your expected result first. For configuration examples, compare compiler output and diagnostics; for test examples, make the assertion fail deliberately to verify that the tool reports it.

Check your understanding

Which statement best describes the role of TypeScript and the actual runtime for this lesson?
Choose one answer. You can try again.

Answer and explanation

The described check or API works only within its stated environment and still needs the corresponding runtime behavior.

parsePort applies a default only if the input is missing. It then checks both integer shape and allowed TCP-port range. process.env.PATH is read safely without printing its value.

What to remember

Environment variables provide configuration passed to a Node process by its parent or deployment environment.

Run the primary example, then complete the practice task or environment-specific lab.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Building Command-Line Applications →