Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Output from a Node.js child process is cut off or the process stops

ErrorERR_CHILD_PROCESS_STDIO_MAXBUFFER

Short answer

exec and execFile hold all output in memory, capped by maxBuffer (1 MiB by default, counted in bytes). Past the cap Node.js terminates the child and truncates what you got. Raise maxBuffer for modest output, or switch to spawn and read stdout as a stream. With spawn, always consume the pipes.

  • Applies to Node.js 22, Node.js 24
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print the error from the callback and the size of what arrived. If the error carries the maxBuffer code and the length is about 1 MiB, cause 1 applies; if there is no error and the child just hangs, go to cause 2.

    const { exec } = require('node:child_process');
    exec('your-command', (err, stdout) => {
      console.log(err && err.code, Buffer.byteLength(stdout));
    });
  2. 2Match what you see, then jump to its fix

    1. 1err.code is ERR_CHILD_PROCESS_STDIO_MAXBUFFER and stdout stops at about 1 MiBOutput is larger than maxBuffer for exec or execFileFix
    2. 2exec is used for a command whose output can be large or has no fixed sizeLarge output should be streamed with spawn, not bufferedFix
    3. 3The child process stays running with no new output and 'close' never firesA spawned child blocks because nobody reads its outputFix

What does the error look like?

  • ERR_CHILD_PROCESS_STDIO_MAXBUFFER
  • stdout is cut in the middle of the output when using exec
  • A spawned child process never exits and its output stops

Cause 1 of 3

Output is larger than maxBuffer for exec or execFile

1Check

The default maxBuffer for exec and execFile is 1024 * 1024 bytes for stdout and stderr each. The limit counts bytes, not characters, so text in UTF-8 with non-Latin characters reaches it sooner than the character count suggests.

your-command | wc -c

2Fix

If the output is bounded, set a larger maxBuffer. If it can be large or endless, use spawn instead (next cause).

exec('your-command', { maxBuffer: 50 * 1024 * 1024 }, (err, stdout) => {
  if (err) throw err;
  console.log(stdout.length);
});

3Verify

Run the same command again. err should be null and stdout should end where the real output ends.

node run.js | tail -n 3

Confirmed by nodejs.org

Did this fix it?

Cause 2 of 3

Large output should be streamed with spawn, not buffered

1Check

Search the code for exec and execFile calls that run commands printing logs, dumps or file listings.

grep -rn "exec(\|execFile(" --include=*.js src

2Fix

spawn has no output buffer. Handle each chunk as it arrives and write it to a file or a stream.

const { spawn } = require('node:child_process');
const fs = require('node:fs');

const child = spawn('your-command', ['--flag']);
child.stdout.pipe(fs.createWriteStream('out.txt'));
child.stderr.on('data', (d) => process.stderr.write(d));
child.on('close', (code) => console.log('exit', code));

3Verify

The file holds the full output and the process memory stays flat.

wc -c out.txt

Confirmed by nodejs.org

Did this fix it?

Cause 3 of 3

A spawned child blocks because nobody reads its output

1Check

If a child writes more to stdout than the pipe holds and the parent does not read it, the child waits for the pipe to drain. Look for spawn calls with no stdout listener or pipe.

grep -rn "spawn(" --include=*.js src

2Fix

Read the stream (a data listener or pipe), or tell Node.js to discard it with stdio ignore when you do not need the output.

// output not needed
spawn('your-command', [], { stdio: 'ignore' });

// output needed
const child = spawn('your-command');
child.stdout.on('data', (chunk) => handle(chunk));

3Verify

The close event now fires and the exit code is logged.

node run.js

Confirmed by nodejs.org

Did this fix it?

How do I stop it coming back?

  • Use spawn for any command whose output size you do not control.
  • Always attach a listener to stdout and stderr, or set stdio to ignore.
  • Treat maxBuffer as a safety limit, and handle the error in the exec callback.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

ng serve says it cannot determine the project or target

Cannot determine project for command.

The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.

3 causes, each with a fix

Back end

ng serve cannot find the dev-server builder package

Could not find the implementation for builder @angular/build:dev-server

The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.