WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Node.js · Intermediate · question 37 of 100

Explain the differences between ’spawn’, ’exec’, and ’fork’ methods in the ’child_process’ module of Node.js.?

📕 Buy this interview preparation book: 100 Node.js questions & answers — PDF + EPUB for $5

The ‘child_process‘ module in Node.js allows you to create and manage child processes. It provides three methods, ‘spawn‘, ‘exec‘, and ‘fork‘, to start new processes with different levels of control and communication abilities. Let’s discuss each method in detail:

1. ‘spawn‘

‘spawn‘ is the most basic method of creating a child process in Node.js. It is used to launch a new process with a given command and returns a ‘ChildProcess‘ object that represents the newly created process.

Syntax:

const spawn = require('child_process').spawn;
const child = spawn(command, [args], options);

‘spawn‘ has several advantages:

- It is suitable for long-running, data-intensive processes since the data is continuously streamed to the parent process.

- It does not create a shell which makes it faster and more efficient.

Example:

const { spawn } = require('child_process');
const child = spawn('ls', ['-la']);

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

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

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

2. ‘exec‘

‘exec‘ is a higher-level method that creates a shell and runs a command within that shell, buffering any generated output in the memory. When the process is done, a callback function is called with the output data.

Syntax:

const exec = require('child_process').exec;
const child = exec(command, options, callback);

‘exec‘ has some notable differences:

- It is suitable for short-running processes that do not generate a lot of output.

- It automatically creates a shell to execute the command, which may add some overhead.

- It buffers the output and passes it to a callback function after the process completes.

Example:

const { exec } = require('child_process');
exec('ls -la', (error, stdout, stderr) => {
  if (error) {
    console.error(`exec error: ${error}`);
    return;
  }
  console.log(`stdout: ${stdout}`);
  console.error(`stderr: ${stderr}`);
});

3. ‘fork‘

‘fork‘ is a special case of ‘spawn‘ which is specifically designed for spawning Node.js processes. It creates a new instance of the Node.js process, executing a specified JavaScript module.

Syntax:

const fork = require('child_process').fork;
const child = fork(modulePath, [args], options);

‘fork‘ has some unique features:

- It has built-in support for IPC (Inter-Process Communication).

- It is specifically designed for spawning new Node.js processes, running the specified JavaScript module.

- Similar to ‘spawn‘, it does not create a shell and is thus more efficient.

Example:

‘parent.js‘:

const { fork } = require('child_process');
const child = fork('./child.js');

child.on('message', (message) => {
  console.log(`Message from child: ${message}`);
});

child.send('Hello from parent');

‘child.js‘:

process.on('message', (message) => {
  console.log(`Message from parent: ${message}`);
});

process.send('Hello from child');

In summary:

- ‘spawn‘ is a low-level method for launching processes without a shell, suited for long-running processes that continuously generate output.

- ‘exec‘ is a higher-level method for running shell commands, suited for short-running processes and automatically buffering the output.

- ‘fork‘ is a special case of ‘spawn‘ that is designed specifically for spawning new Node.js processes, providing built-in IPC support.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Node.js interview — then scores it.
📞 Practice Node.js — free 15 min
📕 Buy this interview preparation book: 100 Node.js questions & answers — PDF + EPUB for $5

All 100 Node.js questions · All topics