0
0
Node.jsframework~5 mins

spawn for streaming processes in Node.js

Choose your learning style9 modes available
Introduction

We use spawn to run other programs from Node.js and handle their output as it happens, like watching a live stream.

You want to run a command-line tool and process its output bit by bit.
You need to handle large data from another program without waiting for it all to finish.
You want to show progress or logs from a running process in real time.
You want to run a long-running task and react to its output immediately.
Syntax
Node.js
const { spawn } = require('child_process');
const child = spawn(command, args, options);

child.stdout.on('data', (data) => {
  // handle output data chunk
});

child.stderr.on('data', (data) => {
  // handle error output chunk
});

child.on('close', (code) => {
  // process finished with exit code
});

command is the program you want to run, like 'ls' or 'ping'.

args is an array of strings with arguments for the command.

Examples
This runs the ls -lh /usr command and prints its output as it comes.
Node.js
const { spawn } = require('child_process');
const ls = spawn('ls', ['-lh', '/usr']);

ls.stdout.on('data', (data) => {
  console.log(`Output: ${data}`);
});
This runs ping to send 4 packets and prints each response as it arrives.
Node.js
const { spawn } = require('child_process');
const ping = spawn('ping', ['-c', '4', 'google.com']);

ping.stdout.on('data', (data) => {
  console.log(`Ping output: ${data}`);
});
Sample Program

This program runs node -v to get the Node.js version installed. It prints the version as soon as it receives it, handles any errors, and shows when the process ends.

Node.js
const { spawn } = require('child_process');

// Run 'node -v' to get Node.js version
const child = spawn('node', ['-v']);

child.stdout.on('data', (data) => {
  console.log(`Node version: ${data.toString()}`);
});

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

child.on('close', (code) => {
  console.log(`Process exited with code ${code}`);
});
OutputSuccess
Important Notes

Use streams: spawn lets you read output as a stream, so you don't wait for the whole process to finish.

Handle errors: Always listen to stderr to catch errors from the spawned process.

Close event: The close event tells you when the process ends and its exit code.

Summary

spawn runs commands and streams their output live.

It is useful for handling big or ongoing outputs without delay.

Always listen to stdout, stderr, and close events.