Output from a Node.js child process is cut off or the process stops
ERR_CHILD_PROCESS_STDIO_MAXBUFFERShort 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.
Fix it fast
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)); });2Match what you see, then jump to its fix
- 1
err.code is ERR_CHILD_PROCESS_STDIO_MAXBUFFER and stdout stops at about 1 MiBOutput is larger than maxBuffer for exec or execFileFix - 2
exec is used for a command whose output can be large or has no fixed sizeLarge output should be streamed with spawn, not bufferedFix - 3
The child process stays running with no new output and 'close' never firesA spawned child blocks because nobody reads its outputFix
- 1
What does the error look like?
ERR_CHILD_PROCESS_STDIO_MAXBUFFERstdout is cut in the middle of the output when using execA 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 -c2Fix
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 3Confirmed by nodejs.org
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 src2Fix
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.txtConfirmed by nodejs.org
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 src2Fix
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.jsConfirmed by nodejs.org
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 fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack 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 fixBack end
Angular build says it cannot resolve '@angular/localize/init'
Can't resolve '@angular/localize/init' in '/usr/src/app/src'
The build imports @angular/localize, and that package is not in node_modules.
2 causes, each with a fixBack end
The command npm run build -- --prod exited with code 1 (Angular)
The command "npm run build -- --prod" exited with code 1
Exit code 1 only says the build failed; the real message is above it.
2 causes, each with a fix