10X Sale
kh logo
All Courses
  1. Tutorials
  2. Web Development

Child Process

Updated on Aug 29, 2025
 
14,484 Views

Node.js comes with a child process module, which helps to run other processes in the current environment i.e. it can be used to communicate with other processes.

Let’s understand this with an example.

App1:

- Create a new file exec.js and import child_process module.
- const cp = require(“child_process”);
- The child process has a method exec to open another process.
- cp.exec(“open http://www.google.com”);
- run node exec
- And observe that google page is opened.
- Imagine if we invoke a process that returns data then we can grab that data as well.

cp.exec(“ls”, (err, data) => { 
 if (err) { throw err; } 
Console.log(data); 
});

- In step 5, we are listing down all the files because of ls command. If we spell it wrongly say listFiles then we get an error saying listFiles: command not found.
- Error object has all the stack trace content. If we want just the brief note on an error then we can rely on 3rd parameter of the callback ‘stderr’.
- cp.exec(“ls”, (err, data, stderr) => {

console.log(stderr); 
});

- So, cp.exec can execute any process. From the previous example, we were running node readStream. Even this can be executed from the child process exec command.

cp.exec(“node readStream”,);

Exec is for invoking the other processes and capturing their output. This happens in a synchronous way. To handle the async way, we need to understand the spawn method of the child process.

During HandlingData module, we create a node.js app which displays a set of questions. It starts displaying the first question and on answering it, we are been asked the second question and so on until all questions are answered. This app was working in an async way and reacting to user inputs.

Lets copy that file and rename to question.js

App2:

- lets create spawn.js file and import child_process.
- const cp = require(“child_process”);
- Spawn the question app using spawn method of child process.
- const questionApp = cp.spawn(“node”, [“questions.js”]);
- Also, let’s listen to the stdout of the question app and populate it.

questionApp.stdout.on(“data”, data => {
console.log(`from question app: ${data}`); 
});

- Run the node spawn.js
- Observe that questionApp is executed and first question is displayed.
- But the terminal is not taking any inputs.
- Let's capture the input from the current terminal and send it to the question app.

Process.stdin.on(“data”, data => {
questionApp.stdin.write(`${data}` \n’); 
}); 
When question app is closed, lets close our current app as well by listening to close event.
questionApp.on(“close”, () => { 
console.log(“questionApp process is exited”); 
process.exit(); 
});

- Run node spawn.js
- Terminal should display each question after another on answering from question app. On completion of all questions, process has to exit.

constcp = require("child_process"); 
constquestionApp = cp.spawn("node", ["questions.js"]); 
questionApp.stdout.on("data", data=> { 
console.log(`from the question app: ${data}`); 
}); 
process.stdin.on("data", data=> { 
questionApp.stdin.write(`${data}\n\n`); 
}); 
questionApp.on("close", () => { 
console.log(`questionApp process exited`); 
process.exit(); 
}); 
Output: 
from the question app: 
 What is your name? > 
Trainer 
from the question app: 
 What would you rather be doing? > 
Training 
from the question app: 
 What is your preferred programming language? > 
Javascript 
from the question app: 
 Training Trainer, Javascript is awesome! 
questionApp process exited
+91

By Signing up, you agree to ourTerms & Conditionsand ourPrivacy and Policy

Get your free handbook for CSM!!
Recommended Courses