Junior From PDF Node.js Node.js

What is the difference between spawn() and exec()?

Both create child processes, but:

  • spawn() streams data (good for large outputs)
  • exec() buffers data (good for small outputs)
const { spawn, exec } = require('child_process');

// spawn example

const ls = spawn('ls', ['-lh', '/usr']);
ls.stdout.on('data', (data) => console.log(`Output: ${data}`));

// exec example

exec('ls -lh /usr', (error, stdout) => console.log(stdout));

More from Node.js Tutorial

All questions for this course
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details