Configuration and exitCode
Local practice: Node.js 22.16.0 and npm 10.9.2 on Linux x64; proposed teaching baseline 26.13.1 LTS remains untested. Use a fresh disposable folder for each example, copy only the shown files and run the fixed command. No packages or external services required. Static examples are not ByteMind runner support. The archive is author-only; do not copy expected.json into the example directory.
Load synthetic configuration
import process from "node:process";const raw = process.env.REPORT_FORMAT ?? "text";console.log(raw === "json" ? "JSON output" : "Text output");Run in this fixture directory:
node --env-file=fixture.env main.mjsExpected stdout:
JSON outputExpected exit status: 0. Expected stderr: empty. The flag loads the synthetic file in this fixture; the value is compared as a string.
How it works
Use named, narrowly scoped configuration inputs. process.cwd() reports the directory from which relative filesystem paths are resolved, not necessarily the script directory. A .env file is text configuration; Node 22.16.0 supports --env-file for loading it (existing environment values take precedence). Do not commit credentials; use approved secret management for production. process.exitCode = 1 requests a failing status when the process naturally exits, while process.exit() can terminate before pending output completes. A live timer or open handle can delay natural exit. globalThis accesses the global object; local module bindings are not automatically globals.
Reject an invalid boolean setting
import process from "node:process";const raw = process.env.REPORT_VERBOSE;if (raw !== "true" && raw !== "false") { console.error("REPORT_VERBOSE must be true or false"); process.exitCode = 1;} else { console.log(raw === "true");}Run in this fixture directory:
node --env-file=fixture.env main.mjsExpected stdout: (empty).
Expected exit status: 1. Expected stderr contains REPORT_VERBOSE must be true or false. The configuration contract rejects the string yes; a failure status is returned without forcing immediate termination.
Common mistake
Do not rely on if (process.env.FEATURE) to interpret the string "false". Accept documented values explicitly, and never dump the whole environment for diagnostics.
Try it yourself
Add a required REPORT_LIMIT variable accepting integers 1 through 50, with a specific error for missing and invalid values. Test boundary values without reading real credentials.
Further reading: Official documentation 1, Official documentation 2. Related ByteMind reference candidate: node-process-exitcode (route not verified).
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Configuration and exitCode — knowledge check
Apply the concepts and predict the behavior taught in this lesson.
What is the type of an ordinary configured process.env value?
Why prefer exitCode for a validation failure?
What does process.cwd() identify?