Console and process output
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.
Print a JSON result and diagnostic
console.log(JSON.stringify({ items: 2 }));console.error("Report generated from fixture");Run in this fixture directory:
node main.mjsExpected stdout:
{"items":2}Expected exit status: 0. Expected stderr contains Report generated from fixture. The report can be parsed independently of the diagnostic.
How it works
console.log() normally writes formatted text plus a newline to stdout; console.error() normally writes to stderr. process.stdout.write() writes the supplied string or bytes without adding a newline. A nonzero exit status indicates a failure by convention, but stderr alone does not determine exit status. Output writes can be synchronous or asynchronous depending on platform and destination; do not rely on the same timing everywhere. Keep machine-readable results on stdout and progress/errors on stderr.
Control line endings
import process from "node:process";process.stdout.write("Book");process.stdout.write(": 3\n");Run in this fixture directory:
node main.mjsExpected stdout:
Book: 3Expected exit status: 0. Expected stderr: empty. write does not add a newline; the second call includes it explicitly.
Common mistake
Do not interleave debug lines into a JSON output contract. A downstream parser expects only the promised JSON. Never print tokens or whole environment objects when debugging.
Try it yourself
Produce a JSON report for two fictional books and a diagnostic on stderr. Capture the channels independently and verify stdout parses as JSON.
Further reading: Official documentation 1, Official documentation 2. Related ByteMind reference candidate: node-process-stdout-write (route not verified).
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Console and process output — knowledge check
Apply the concepts and predict the behavior taught in this lesson.
Which stream should contain a promised JSON result?
Does console.error alone set a nonzero exit status?
Which method writes without automatically appending a newline?