codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🟢

Node.js

13 / 63 topics
13Child Processes14Clusters15Async Programming16Promises17Async/Await18Error Handling
Tutorials/Node.js/Child Processes
🟢Node.js

Child Processes

Updated 2026-05-15
10 min read

Child Processes

Introduction

In the world of Node.js, child processes are a powerful tool that allow you to spawn new processes and interact with them. This can be particularly useful for running shell commands, executing external scripts, or managing long-running tasks in parallel. Understanding how to create and manage child processes is essential for building robust and scalable applications.

Concept

Node.js provides the child_process module, which includes several methods to spawn new processes:

  • spawn(command[, args][, options]): Spawns a new process using the given command.
  • exec(command[, options][, callback]): Executes a command in a shell and buffers the output.
  • execFile(file[, args][, options][, callback]): Similar to exec, but does not spawn a shell by default.
  • fork(modulePath[, args][, options]): Spawns a new Node.js process with the given module path.

Each method has its own use case and characteristics. For instance, spawn is ideal for long-running processes that generate a lot of output, while exec is better suited for short-lived commands where you need to capture the output in one go.

Examples

Basic Usage with spawn

Let's start by using the spawn method to execute a simple shell command. We'll run the ls -la command to list directory contents.

JavaScript
1const { spawn } = require('child_process');
2
3const ls = spawn('ls', ['-la']);
4
5ls.stdout.on('data', (data) => {
6console.log(`stdout: ${data}`);
7});
8
9ls.stderr.on('data', (data) => {
10console.error(`stderr: ${data}`);
11});
12
13ls.on('close', (code) => {
14console.log(`child process exited with code ${code}`);
15});
Terminal
$ node your-script.js
Output
stdout: drwxr-xr-x 10 user group 4096 May 15 12:34 .
drwxr-xr-x 3 user group 4096 May 15 12:34 ..
-rw-r--r-- 1 user group 0 May 15 12:34 your-script.js
...
child process exited with code 0

Using exec

The exec method is useful when you need to execute a command and capture its output in one go. Here's an example of using exec to get the current date.

JavaScript
1const { exec } = require('child_process');
2
3exec('date', (error, stdout, stderr) => {
4if (error) {
5 console.error(`exec error: ${error}`);
6 return;
7}
8console.log(`stdout: ${stdout}`);
9console.error(`stderr: ${stderr}`);
10});
Terminal
$ node your-script.js
Output
stdout: Sun May 15 12:34:56 UTC 2026

stderr:

Using fork

The fork method is specifically designed to spawn new Node.js processes. It allows for inter-process communication (IPC) between the parent and child process, which can be very useful for distributed systems.

JavaScript
1// parent.js
2const { fork } = require('child_process');
3
4const child = fork('child.js');
5
6child.on('message', (msg) => {
7console.log('Message from child:', msg);
8});
9
10child.send({ hello: 'world' });
JavaScript
1// child.js
2process.on('message', (msg) => {
3console.log('Message from parent:', msg);
4process.send({ response: 'Hello from child!' });
5});
Terminal
$ node parent.js
Output
Message from parent: { hello: 'world' }
Message from child: { response: 'Hello from child!' }

What's Next?

Now that you have a good understanding of how to create and manage child processes in Node.js, the next step is to explore Clusters. Clusters allow you to take advantage of multi-core systems by creating multiple worker processes that share the same server port.

Stay tuned for more advanced topics on Node.js!

Info

Remember, managing child processes can be complex, so always handle errors and edge cases appropriately.


PreviousOS ModuleNext Clusters

Recommended Gear

OS ModuleClusters