Understanding Node.js Child Process

Shubham SharmaShubham Sharma
5 min read

In this blog post, we're going to explore the Node.js child_process module. We'll start by understanding what this child_process module is and why it's valuable in Node.js. Then, we'll get hands-on with practical examples that show you exactly how its methods work. So, let's get started.

What Is a Child Process?

To understand what the child process is, let's first understand why it's required, and what is the need for a child process in node.js?

Node.js runs in a single process on a single thread. But what if we need to create a new process from Node.js? Here comes the Node.js child_process module that provides us with the ability to create and manage a new process (called a child process).

Using the child_process module, we can spawn subprocesses (a function that loads and executes a new child_process), spawn a shell and run commands in the shell, and also execute external scripts and files.

Child Process Methods

Now it should be clear to you why child_process is required and some of its uses. Let's now see what methods are provided by node.js child_process module:

  1. spawn()

  2. exec()

  3. execFile()

  4. fork()

All these methods are used to create child processes asynchronously; for specific use cases, you can also create processes synchronously using these methods: spawnSync(), execSync() and execFileSync().

Let's look at each method one by one.

child_process.spawn()

child_process.spawn(command[,args][, options]) method spawns a new process using the given command, with command line arguments and additional options if provided.

Let's see an example of executing a given command ls (Unix/Linux File listing) using child_process.spawn():

The above program executes ls the command with the arguments ['-l', '-a'](-l means Long format and -a means all files including hidden files) and captures the data in streams stdout(i.e. output data), stderr(i.e. error data).

Similarly to the above example, execute any command you like with the help of spawn() method, and you can capture its output in streams. If needed, you can also pass additional options.

The rest of the methods(exec(), execFile(), fork()) are implemented on top of spawn().

child_process.exec()

child_process.exec(command[, options][, callback]) spawns a new shell and then executes the given command in the shell, all the arguments will be passed in the command with space as a separator. The output will be stored in a buffer and provided via callback if used. Callback contains three arguments error (with code and signal), stdout and stderr with the output data.

Let's run the same above example by adding one option cwd and callback using child_process.exec():

When you compare the exec() method with spawn() there are a few differences in the above program:

  • The callback is used to hold the output data generated by child_process.

  • All the arguments are provided inside the command with space as a separator.

cwd is the current working directory specified in the options section of the exec() method, here we are specifying process.env.HOME i.e. user home directory as the cwd. Upon execution of the above program, all the files/directories of the user will be printed to the console.

child_process.execFile()

child_process.execFile(file,[,args][, options][, callback]) is similar to the child_process.exec() method except that it doesn't spawn a new shell, it directly spawns the specified executable file as a new process with given arguments and options.

execFile() is slightly more efficient than child_process.exec() as it doesn't have to spawn a new shell every time.

It's useful when you need to execute the binary file and capture its output or to execute a binary program.

Let's see an example of executing a Python script from node.js using child_process.execFile():

In the above program, we are using execFile() to execute the python script from node, we are passing the command python, script path and parameters in the arguments to execFile(). Similar to the exec() it gives returns output in callback.

Similar to executing a Python script, we can use execFile() to execute database backup operations, docker operations, git operations, etc.

We can use Promise version of child_process.exec() and child_process.execFile() using util.promisify().

child_process.fork()

child_process.fork(modulePath,[,args][, options]) is similar to, ⁣ butchild_process.spawn() used particularly to, spawn node.js process. It returns the ChildProcess class object with communication channel established, which can be used for communication between parent and child using IPC.

The spawned Node.js child process is independent of the parent process and allows communication using IPC. It has its dedicated memory and V8 Instance.

Let's understand fork() with an example:

To understand what's happening, let's break down the above program:

  • The program generates a hash of a given password using hash_worker.js.

  • fork() method takes in node.js program (hash_worker.js) and returns hashWorker which is used to communicate with the child.

  • For communication, child_process provides send() method and here hashWorker.send(passwordToHash); is used to send a message to the child for generating the hash.

  • hash_worker.js generates a hash and returns the hashed password using process.send(msg) method.

  • The parent receives the hashed password using hashWorker.on('message',(msg)=>{}).

The hash_worker.js uses Node.js crypto module for hash generation, which is a CPU-intensive operation

We can also use the child_process.fork() method to perform the following CPU-intensive operations, while still utilizing the IPC communication channel:

  • encryption/decryption/hashing

  • complex mathematical calculations

  • complex regular expression

  • image/audio/video processing, compression and decompression, etc

For all the above operations, we can utilize the IPC communication channel for communication between child and parent process.

In this blog post, I have covered all the asynchronous methods provided by Node.js child_process module. I have also included practical examples to make things clear.

Now, you should have a good idea of what the child_process module is, why it's useful in Node.js, and how its methods work. As a part of my learning, I have aimed to make this topic easy to understand, so you can use the child_process module effectively in your own projects.


0
Subscribe to my newsletter

Read articles from Shubham Sharma directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Shubham Sharma
Shubham Sharma

I am a seasoned software engineer with over 5 years of experience, specializing in JavaScript technologies like Node.js and React.js. My passion for technology fuels my drive to continuously learn, adapt, and create impactful solutions that meet the needs of users and businesses. I excel in designing and developing high-performance, secure, and scalable components and APIs. Collaboration and clean code are at the core of my approach, ensuring that I deliver value to clients through best practices and a customer-centric mindset.