Node.js Child Process Module

Prepare for Node.js backend interviews and deepen your understanding of process management with this comprehensive flashcard collection focused on the Child Process module. This set covers everything developers need to know about creating and managing subprocesses in Node.js, including process spawning, command execution, inter-process communication (IPC), signal handling, process lifecycle events, and advanced serialization mechanisms. Topics included: • ChildProcess class and lifecycle • spawn(), fork(), exec(), and execFile() • Synchronous vs asynchronous process creation • IPC channels and message passing • process.send() and subprocess.send() • Child process events: spawn, exit, close, error, disconnect, message • Process identifiers (PID) and exit codes • Signal handling and process termination • Detached processes and background execution • stdio configuration and stream management • stdin, stdout, and stderr handling • Child process references and unref()/ref() behavior • Forking Node.js applications • Structured clone serialization and V8-based IPC • Windows-specific process execution behavior • Performance implications of synchronous APIs • Passing TCP servers and sockets between processes • Error handling and process monitoring Whether you are preparing for Node.js, NestJS, backend, full-stack, or senior engineering interviews, these flashcards help reinforce critical concepts frequently asked during technical assessments and system design discussions. Ideal for software engineers, backend developers, team leads, and architects who want to build reliable, scalable, and production-ready Node.js applications using multi-process architectures. You may also be interested in: Master Node.js: From Basics to Production Node.js Stream Module Node.js Cluster Module Node.js Buffer API Interview Questions Node.js Crypto Module Node.js Operating System Module Node.js File System Module

You can start studying this pack. You'll receive 180 messages to help you review each card multiple times, following the principles of the forgetting curve.

Learn more about the spaced repetition method.
Child process

The node:child_process module provides the ability to spawn subprocesses in a manner that is similar, but not identical, to popen(3).


This capability is primarily provided by the child_process.spawn() function.

1/45
Asynchronous process creation

The child_process.spawn(), child_process.fork(), child_process.exec(), and child_process.execFile() methods all follow the idiomatic asynchronous programming pattern typical of other Node.js APIs.

2/45
Spawning .bat and .cmd files on Windows

The importance of the distinction between child_process.exec() and child_process.execFile() can vary based on platform.

3/45
child_process.exec

Spawns a shell and runs a command within that shell, passing the stdout and stderr to a callback function when complete.

4/45
child_process.exec callback

If a callback function is provided, it is called with the arguments (error, stdout, stderr).


On success, error will be null.


On error, error will be an instance of Error.

5/45
child_process.exec signal

If the signal option is enabled, calling .abort() on the corresponding AbortController is similar to calling .kill() on the child process except the error passed to the callback will be an AbortError.

6/45
child_process.execFile

Similar to child_process.exec() except that it spawns the command directly without first spawning a shell by default.

7/45
child_process.fork

Spawns a new Node.js process and invokes a specified module with an IPC communication channel established that allows sending messages between parent and child.

8/45
child_process.spawn

The method spawns a new process using the given command, with command-line arguments in args.


If omitted, args defaults to an empty array.

9/45
child_process.spawn examples

Example of running ls -lh /usr.


A very elaborate way to run ps ax | grep ssh


Example of checking for failed spawn.

10/45
options.detached

On Windows, setting options.detached to true makes it possible for the child process to continue running after the parent exits. The child process will have its own console window. Once enabled for a child process, it cannot be disabled.

11/45
options.stdio

The options.stdio option is used to configure the pipes that are established between the parent and child process.

12/45
Synchronous process creation

The child_process.spawnSync(), child_process.execSync(), and child_process.execFileSync() methods are synchronous and will block the Node.js event loop, pausing execution of any additional code until the spawned process exits.

13/45
child_process.execFileSync

A synchronous version of child_process.execFile() that will block the Node.js event loop.

14/45
child_process.execSync

A synchronous version of child_process.exec() that will block the Node.js event loop.

15/45
child_process.spawnSync

The method is generally identical to child_process.spawn() with the exception that the function will not return until the child process has fully closed.

16/45
Class: ChildProcess

Extends: <EventEmitter>


Instances of the ChildProcess represent spawned child processes.

17/45
Event: close

The event is emitted after a process has ended and the stdio streams of a child process have been closed.

18/45
Event: disconnect

The event is emitted after calling the subprocess.disconnect() method in parent process or process.disconnect() in child process.

19/45
Event: error

The event may or may not fire after an error has occurred. When listening to both the 'exit' and 'error' events, guard against accidentally invoking handler functions multiple times.

20/45
Event: exit

The event is emitted after the child process ends.

21/45
Event: message

The event is triggered when a child process uses process.send() to send messages.

22/45
Event: spawn

The event is emitted once the child process has spawned successfully.

23/45
subprocess.channel

<Object> A pipe representing the IPC channel to the child process.


The subprocess.channel property is a reference to the child's IPC channel.


If no IPC channel exists, this property is undefined.

24/45
subprocess.channel.ref()

This method makes the IPC channel keep the event loop of the parent process running if .unref() has been called before.

25/45
subprocess.channel.unref()

This method makes the IPC channel not keep the event loop of the parent process running, and lets it finish even while the channel is open.

26/45
subprocess.connected

<boolean> Set to false after subprocess.disconnect() is called.


The subprocess.connected property indicates whether it is still possible to send and receive messages from a child process.

27/45
subprocess.disconnect()

Closes the IPC channel between parent and child processes, allowing the child process to exit gracefully once there are no other connections keeping it alive.

28/45
subprocess.exitCode

The property indicates the exit code of the child process.


If the child process is still running, the field will be null.

29/45
subprocess.kill([signal])

The method sends a signal to the child process.

30/45
subprocess.killed

<boolean> Set to true after subprocess.kill() is used to successfully send a signal to the child process.


The property indicates whether the child process successfully received a signal from subprocess.kill().

31/45
subprocess.pid

<integer> | <undefined>


Returns the process identifier (PID) of the child process.


If the child process fails to spawn due to errors, then the value is undefined and error is emitted.

32/45
subprocess.ref()

Calling subprocess.ref() after making a call to subprocess.unref() will restore the removed reference count for the child process, forcing the parent process to wait for the child process to exit before exiting itself.

33/45
subprocess.send()

The subprocess.send() method can be used to send messages to the child process.

34/45
Example: sending a server object

The sendHandle argument can be used, for instance, to pass the handle of a TCP server object to the child process as illustrated in the example below.

35/45
Example: sending a socket object

Similarly, the sendHandler argument can be used to pass the handle of a socket to the child process. The example below spawns two children that each handle connections with "normal" or "special" priority.

36/45
subprocess.signalCode

<string> | <null>


The subprocess.signalCode property indicates the signal received by the child process if any, else null.

37/45
subprocess.spawnargs

<Array>


The subprocess.spawnargs property represents the full list of command-line arguments the child process was launched with.

38/45
subprocess.spawnfile

<string>


The subprocess.spawnfile property indicates the executable file name of the child process that is launched.

39/45
subprocess.stderr

<stream.Readable> | <null> | <undefined>


A Readable Stream that represents the child process's stderr.

40/45
subprocess.stdin

<stream.Writable> | <null> | <undefined>


A Writable Stream that represents the child process's stdin.

41/45
subprocess.stdio

<Array>


A sparse array of pipes to the child process, corresponding with positions in the stdio option passed to child_process.spawn().

42/45
subprocess.stdout

<stream.Readable> | <null> | <undefined>


A Readable Stream that represents the child process's stdout.

43/45
subprocess.unref()

By default, the parent process will wait for the detached child process to exit. To prevent the parent process from waiting for a given subprocess to exit, use the subprocess.unref() method.

44/45
Advanced serialization

Child processes support a serialization mechanism for IPC that is based on the serialization API of the node:v8 module, based on the HTML structured clone algorithm.

45/45
WitSlice © 2026