Sub topic 7
Writing to stdout and stderr
Assume you know basic TypeScript, async and await, Node.js, npm, and how to run commands in a terminal. This chapter focuses on output from a command-line program. You will use the same ideas when the program later reads files or reports errors.
A command-line process normally has two output streams:
stdoutcarries the command's normal result.stderrcarries diagnostic messages, warnings, and errors.
A stream is a sequence of data written over time. The terminal usually displays both streams together, so they can look identical. Shell commands treat them differently, however. Users can redirect or pipe stdout without also redirecting stderr.
Choosing the right stream
Use stdout for output that another command or script might consume. Use stderr for information about how the command ran.
| Message | Stream | Example |
|---|---|---|
| A file-counting result | `stdout` | `2 3 notes.txt` |
| Usage instructions after invalid input | `stderr` | `usage: wcl <file>` |
| A missing-file error | `stderr` | `wcl: notes.txt: no such file` |
console.log writes a line to stdout. console.error writes a line to stderr. These functions are convenient for human-readable messages:
console.log("2 3 notes.txt");
console.error("wcl: notes.txt: no such file");For a small command, this distinction is usually enough. Use process.stdout.write and process.stderr.write when you need exact control over newlines or want to make the stream explicit.
process.stdout.write("2 3 notes.txt\n");
process.stderr.write("warning: empty input\n");The \n is important. console.log adds a newline automatically, but write does not.
Seeing the difference in a shell
Suppose wcl prints a result on stdout and an error on stderr. This command saves only the normal result:
wcl notes.txt > result.txtThe shell operator > redirects file descriptor 1, which is the standard output stream. An error still appears in the terminal.
You can redirect the error stream separately:
wcl missing.txt > result.txt 2> errors.txtHere, 2> redirects file descriptor 2, which is standard error. You can combine both streams when you explicitly want one file:
wcl notes.txt > all-output.txt 2>&1The order matters. 2>&1 means “send standard error to wherever standard output currently goes.”
This separation lets scripts safely process results:
wcl notes.txt | sortOnly stdout enters sort. Diagnostic messages remain visible in the terminal instead of becoming input to the next command.
Keep output formats stable
A command-line tool often has two audiences:
- People need readable messages.
- Other programs need predictable data.
Keep machine-readable results on stdout and avoid adding labels or progress messages there. For example, this is easy to parse:
2 3 notes.txtThis is harder to parse because it mixes explanation with data:
The result is: 2 lines and 3 words in notes.txtPut extra explanation on stderr if it does not belong to the result.
A typed output helper
Centralizing output makes the convention visible and testable:
type Result = {
lines: number;
words: number;
file: string;
};
function printResult(result: Result): void {
process.stdout.write(
`${result.lines} ${result.words} ${result.file}\n`,
);
}
function printError(message: string): void {
process.stderr.write(`wcl: ${message}\n`);
}
printResult({ lines: 2, words: 3, file: "notes.txt" });
printError("could not read configuration");The result function writes only data to stdout. The error function adds the program name and writes only diagnostics to stderr.
Exercise
Which stream should receive a count that another command will process, and which stream should receive “file not found”?
Show answer
Send the count to stdout and the error message to stderr.