Typorithm · medium

process & env

process-envnodejsprocessenvshutdown
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn process & env

0%

HTTP & Events

process is the global handle on the running Node program: process.env reads config strings, process.argv holds command-line arguments, process.on("SIGINT") catches Ctrl+C for graceful shutdown, and process.exit(code) ends the program with an exit code.

process.env is a map of environment variables — always strings — used for configuration like process.env.PORT; missing keys are undefined.

process.argv is the array of command-line arguments: index 0 is the node binary, index 1 the script, and index 2 onward are user arguments.

process.on("SIGINT", ...) intercepts Ctrl+C so you can close connections before process.exit(code) ends the program — a non-zero code signals failure.

01

process.env.PORT reads config as a string.

02

process.argv[2] is the first user CLI argument.

03

process.on("SIGINT") enables graceful shutdown before process.exit.

const port = process.env.PORT
const flag = process.argv[2]

process.on("SIGINT", () => {
  process.exit(1)
})

Read the implementation

Connect each part of the code to the concept before you type.

1. Read an env var

process.env exposes environment variables as strings; PORT is a common one for config.

process.env.PORT

2. Read a CLI arg

process.argv holds command-line arguments; index 2 is the first user-supplied argument.

process.argv[2]

3. Handle a signal

process.on("SIGINT") catches Ctrl+C so you can clean up before the process ends.

process.on("SIGINT", () => {

4. Exit the process

process.exit(1) ends the process with a non-zero code signalling an error.

process.exit(1)

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

process & env: Lesson & Practice · Typorithm | ApplyDjinn