nodejs
  1. nodejs-child-process

Node.js Child Process

In Node.js, a child process is a process that is spawned from another process (known as the parent process). Child processes allow you to execute system commands and scripts, as well as other Node.js applications. In this tutorial, we'll discuss how to create and use child processes in Node.js.

Syntax

The syntax for creating a child process in Node.js is as follows:

const { spawn } = require('child_process');
const child = spawn('command', ['argument1', 'argument2', ...]);

The command parameter is the name of the command or script that you want to execute, and the arguments parameter is an array of arguments to pass to the command or script.

Example

Let's say we want to execute a Python script called "hello.py" that takes a parameter and outputs a message. Here's how we can implement it in Node.js:

const { spawn } = require('child_process');
const child = spawn('python', ['hello.py', 'World']);

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}`);
});

This code spawns a new Python process and passes it the file name and a parameter "World". Any messages output by the child process are handled by the stdout and stderr streams. The "close" event is fired when the child process exits.

Output

When we run the example code above, the output will be:

stdout: Hello, World!
child process exited with code 0

This is because the Python script we executed output "Hello, World!" to the console, and the child process exited with a code of 0 (indicating success).

Explanation

In the example above, we used the spawn() method of the child_process module to spawn a new Python process and pass it a filename and parameter. We then set up listeners for the stdout, stderr, and close events.

The stdout and stderr events handle any output messages from the child process. The close event is fired when the child process exits, and we print the exit code associated with that process.

Use

Child processes are useful for executing system commands and scripts, as well as other Node.js applications. You can use child processes to run background tasks, create shell scripts, and execute other scripts or applications.

Important Points

  • Child processes can be created using the spawn(), exec(), execFile(), and fork() methods of the child_process module.
  • You can pass arguments and environment variables to a child process.
  • Child processes have a stdin, stdout, and stderr stream that can be used to communicate with the parent process.

Summary

In this tutorial, we discussed how to create and use child processes in Node.js. We covered the syntax, example, output, explanation, use, and important points of child processes in Node.js. With this knowledge, you can now use child processes to execute system commands and scripts, as well as other Node.js applications, in your Node.js projects.

Published on: