The spawn is a method in NodeJS that spawns an external application through a new process and finally returns a streaming interface for I/O. It is excellent for handling applications that produce a large amount of data or for working with streams of data as it reads in.

BY Best Interview Question ON 10 Jul 2020


Here’s an example how child process have the ability to use the spawn method:

const { spawn } = require('child_process');
const ls = spawn('ls', ['-lh', '/usr']);

ls.stdout.on('data', (data) => {
  console.log(`stdout: ${data}`);

ls.stderr.on('data', (data) => {
  console.error(`stderr: ${data}`);

ls.on('close', (code) => {
  console.log(`child process exited with code ${code}`);