Node.js Course
Node.js
/
Advanced

Child Processes

Definition

A core Node.js module that provides the ability to spawn new OS processes, execute shell commands, and communicate with them.

Explain Like I'm New

Sometimes you need Node to do something outside of JavaScript, like running a Python script, executing a bash command (`ls -la`), or starting a separate heavy Node script. The `child_process` module lets Node act like a terminal user, typing commands and reading the output.

Real World Example

You build a web interface that converts videos. When the user clicks 'Convert', your Node server uses `exec()` to run the command-line tool `FFmpeg` in the background, converting the video without freezing the web server.

Common Use Cases

  • •Executing system commands
  • •Running Python/Bash scripts from Node
  • •Parallel processing

Interactive Example

Loading...
Console output will appear here...

Interview Questions

basic

  • What module do you require to use child processes?

intermediate

  • What is the difference between `exec` and `spawn`?

Flash Cards

Question

What module?

Click to reveal answer
Answer

`require('child_process')`

Question

exec vs spawn?

Click to reveal answer
Answer

`exec` buffers the entire output and returns it all at once in a callback. It is great for quick commands, but will crash if the output is massive. `spawn` returns a stream, allowing you to process massive amounts of data as it happens.