DEV BOX

'26/11/30 UPDATE

NODE.JS / PROCESS I/O / FIELD NOTES

Node output prints in full in the terminal, so why is it cut short in a pipe?

Why Node CLI output prints in full in the terminal but stops partway in a pipe, checked by sending 1 MiB to a terminal, a file and a pipe on macOS 27.0.1 with Node.js v24.21.0. On POSIX, stdout writes to a pipe are asynchronous, and process.exit() does not wait for them. With process.exitCode instead, everything reached the pipe.

Run a Node CLI in the terminal and its output prints to the end. Send the same command into | jq, | wc -c or another agent script, and the output can stop partway. A reader that parses one JSON document reports a parse error, but a reader that works line by line can process the first part and finish with no error. On this Mac, the same script was sent to a terminal, a file and a pipe, and the bytes that arrived were counted. The cause was not in the code that prints but in the code that exits. By the end you will know what to change in your own code and how to check it afterwards.

The same script, sent to three places

The test script emit.mjs writes 16,384 lines of 64 bytes each, exactly 1,048,576 bytes (1 MiB), with a single console.log. Each line starts with a number from 00001 to 16384, so you can see which line arrived last. First come two versions that differ only in how they end. A third version comes up in the section on the fix.

console.log(text);         // 1,048,576 bytes, 16,384 lines
process.exit(0);           // exit version: end right away
// process.exitCode = 0;   // exitCode version: set the code and leave it

Right after console.log, the script writes one line to stderr saying what stdout is attached to and the value of process.stdout.writableLength. That value counts the bytes of writes that have not finished yet, including any part already handed to the pipe.

The runs were made on 2026-11-09 and 2026-11-10 with macOS 27.0.1 (arm64), Node.js v24.21.0 and jq-1.7.1-apple (the jq that ships with macOS). The shell that ran the commands was not attached to a terminal, so the terminal case was reproduced by giving node a pseudo-terminal (pty) with the script command.

  • Terminal and file (> out.txt): all 1,048,576 bytes arrived, and the last line in the file was number 16384. Right after console.log, writableLength was 0 in both cases: the write had already finished.
  • Pipe (| wc -c): only 65,536 bytes arrived. After console.log returned, the 1,048,576-byte write had not finished (its first part was already in the pipe), and process.exit(0) was called in that state. node exited with code 0.

In the exitCode version, too, the 1,048,576-byte write had not finished right after console.log, yet all 1,048,576 bytes and 16,384 lines came through the pipe. The only difference was whether the process waited for that write to finish.

Terminal output. node -v prints v24.21.0. node emit.mjs exit under a pseudo-terminal from script, counted with wc -c, gives 1048576, with the report line stdout=tty, writableLength=0. node emit.mjs exit > out.txt gives the report line stdout=file, writableLength=0, then [exit 0], wc -c 1048576 and last line 16384. node emit.mjs exit | wc -c gives the report line stdout=pipe, writableLength=1048576, then 65536 and [node exit 0]. node emit.mjs exit 2>/dev/null | tail -n 1 shows last line 01280. node emit.mjs exitcode | wc -c gives the report line writableLength=1048576, then 1048576 and [node exit 0]. node emit.mjs exitcode 2>/dev/null | tail -n 1 shows last line 16384.
When the same script writes 1 MiB with one console.log and calls process.exit(0), the terminal and the file get all of it, and the pipe counted by wc -c gets 65,536 bytes. With process.exitCode, the pipe gets all of it too. An excerpt of the test run's output.

Why only the pipe is cut

The Node.js process documentation has a section on how process.stdout and process.stderr differ from other streams. That section and the process.exit() text read the same, sentence for sentence, in the v24.22.2 and v26.13.0 documentation. Whether a write is synchronous or asynchronous depends on what the stream is attached to and on the operating system.

  • Files: synchronous on both Windows and POSIX
  • Terminals (TTYs): asynchronous on Windows, synchronous on POSIX
  • Pipes and sockets: synchronous on Windows, asynchronous on POSIX

macOS and Linux are on the POSIX side. So when output goes to a terminal or a file, console.log returns after the write is done. When it goes to a pipe and something is left over that the pipe could not take at once, it returns before the write has finished, and the event loop writes the rest afterwards. On this Mac, writableLength right after console.log was 0 for outputs of 1,001 and 65,536 bytes, so the write had finished before the call returned, and it equalled the whole output size for outputs of 65,537, 131,072 and 1,048,576 bytes.

process.exit() ends the process as quickly as possible even when asynchronous work is still pending, and that work includes writes to process.stdout and process.stderr. Code that prints a usage message and calls exit(1) right away is a misuse that can lose output for the same reason.

The runs were made on macOS only, not on Linux or Windows, nor on Node.js v26.

DEV NOTES

It depends on what stdout is attached to

The Node.js table for process.stdout and stderr writes, with what this Mac showed

  • File: > out.txt

    writableLength=0

    • POSIX: sync · Windows: sync
    • console.log returns after the write
    • This Mac: 0 queued, all 1,048,576 arrived
  • Terminal (TTY)

    process.stdout.isTTY === true

    • POSIX: sync · Windows: async
    • Seeing it all here proves nothing for pipes
    • This Mac: 0 queued, all 1,048,576 arrived
  • Pipe or socket: | wc -c

    writableLength=1048576

    • POSIX: async · Windows: sync
    • Large output: console.log returns mid-write
    • process.exit() does not wait for it
    • This Mac (pipe): of 1 MiB, only 65,536 to 131,072 arrived

The synchronous/asynchronous table and the process.exit() text are the same in the Node.js v24.22.2 and v26.13.0 docs (checked 2026-11-09). The values for this Mac come from macOS 27.0.1 and Node.js v24.21.0: 1 MiB written with one console.log, then process.exit(0). Measured with a pipe only, not a socket. Not run on Linux or Windows.

The same console.log is a synchronous or an asynchronous write depending on whether stdout is a file, a terminal or a pipe. On POSIX, pipes and sockets are written asynchronously; on this Mac, output was cut in the pipe when process.exit() followed a large write.

Where it stops is not fixed

The exit version was piped 20 more times. 65,536 bytes arrived 15 times and 131,072 bytes 5 times, and in a separate run with | tail -n 1 the last line was number 01280 (1,280 lines, about 81,920 bytes). The number moved from run to run, but the full 1 MiB never arrived. With the reader starting one second late, it was still 65,536 bytes. The exitCode version gave 1,048,576 bytes all 20 times, and also with the reader that started one second late.

These numbers are only what this Mac showed, and the size of the macOS pipe buffer was not checked.

With only the output size changed and 10 runs per size, all four sizes measured from 1,001 to 65,536 bytes arrived in full every time. A 131,072-byte output arrived as 65,536 bytes all 10 times on the first day, and in full 5 times out of 10 when run again the next day. That is why a CLI can look fine on small test data during development and break on the day real data makes the output larger.

json.mjs prints a JSON array of 16,384 items on one line and calls process.exit(0). Piped into jq length, jq reported a parse error, an unfinished string at line 1, column 65,536, and exited with 5. node exited with 0. A shell script that looks only at node's exit code cannot tell that the output was cut. With the exitCode version, jq read all 16,384 items.

Line-by-line JSON (JSON Lines) may not even raise an error. 16,384 JSON objects on lines of exactly 64 bytes, printed the same way and piped into jq -c .id, were cut on a line boundary (1,024 lines make 65,536 bytes): node and jq both exited with 0 in all 5 runs, and jq handled only 1,024 or 1,280 of them. In a version with uneven line lengths, all 5 runs were cut inside a line, and jq had already printed the first 1,333 items before it reported a parse error and exited with 5.

Terminal output. node emit.mjs exit piped into wc -c 20 times gives 65536 fifteen times and 131072 five times. exitcode gives 1048576 all 20 times. With the reader one second late, exit gives 65536 and exitcode 1048576. flush gives 1048576 all 20 times. node json.mjs exit | jq length prints jq: parse error: Unfinished string at EOF at line 1, column 65536, with node exit 0 and jq exit 5. node json.mjs exitcode | jq length prints 16384, with node exit 0 and jq exit 0.
Over 20 runs, and with a reader that started one second late, the process.exit(0) version never delivered the full 1 MiB. The exitCode version and the version that exits inside the write callback delivered it every time. jq, reading one JSON array, reported a parse error.

How to fix it

The basic fix is not to call process.exit() and only set the exit code in process.exitCode. A Node.js process ends by itself when nothing is left in the event loop, and an unfinished stdout write counts as something left.

But setting process.exitCode does not stop execution. In if (!ok) { printUsage(); process.exit(1); } run(args);, swapping only exit for exitCode makes run(args) run with the bad arguments. In the code below, valid, printUsage, run and result are made-up names for illustration, and this code itself was not run; that exitCode does not stop execution and the write-callback version were checked in separate runs.

// 1) Default: set the exit code and let the process end once the write is done
function main(args) {
  if (!valid(args)) {
    printUsage();
    process.exitCode = 1;
    return;              // exitCode does not stop execution, so leave here
  }
  console.log(JSON.stringify(run(args)));
}
main(process.argv.slice(2));

// 2) Only when you must end now: exit inside the callback of the last write
//    (use the exit code that fits that spot)
process.stdout.write(JSON.stringify(result) + '\n', () => process.exit(0));

Use the second form when an open timer or socket keeps the process from ending on its own. The callback of write() is called once that chunk of data has been handled. The third version in the experiment, the flush version, writes the 1 MiB with one process.stdout.write instead of console.log and calls process.exit(0) inside its callback; all 20 runs delivered 1,048,576 bytes.

When ending because of an error, write the message to stderr with console.error, set process.exitCode = 1 and leave the same way. On this Mac, ending like that after writing 1 MiB delivered 1,048,576 bytes all 10 times. Throwing an uncaught error after the same 1 MiB cut the pipe output at 65,536 or 131,072 bytes in all 20 runs.

Search your CLI for process.exit(, change the calls that come after output to process.exitCode = …, and keep the code after that spot from running: return inside a function, or wrap the rest in if/else at the top level. After the change, look for a cut by comparing a few times, with output well beyond 131,072 bytes (1 MiB, say), the bytes a file receives (node cli.mjs > out.txt; wc -c < out.txt), the bytes a pipe receives (node cli.mjs | wc -c) and the bytes a reader that starts one second late receives (node cli.mjs | (sleep 1; wc -c)). A reader that reads at once, like wc -c, is not enough. With a 1 MiB console.log followed by process.exit(0) in a 100-millisecond timer, wc -c got all of it 5 times out of 5, but the reader that started one second late got only 65,536 bytes all 5 times. Three counts that match every time still do not prove that nothing is cut. With the timer at 1.5 seconds, all three checks got the full 1 MiB, yet a reader that started 0.5 seconds late and read 4 KiB every 20 milliseconds got only 237,568 bytes all 3 times. Comparing byte counts can only find a cut, so the first thing to check is the code: that no process.exit() runs after output outside a write callback.

THE END
RSS feedBack to the library
COMMENTS BOX

Add your perspective.

Share a question, another approach, or something you have tried.

Newest first

Checking sign-in…

Loading comments…