Overview
Theant:child_process module allows you to spawn child processes, execute shell commands, and communicate via stdin/stdout/stderr. All operations are powered by libuv for efficient process management.
Importing
Spawning Processes
Basic Spawn
With Options
Executing Shell Commands
Async Exec
Exec with Options
Sync Exec
Process Events
Standard Output
Standard Error
Exit Event
Close Event
Writing to stdin
Stream Events
Killing Processes
Process Information
Synchronous Spawn
With Input
Forking Node Processes
Reference Management
Keep Process Alive
Stream Control
Piping Example
Error Handling
Complete Example
Best Practices
- Always handle ‘error’ events on child processes
- Use ‘close’ event for cleanup, not ‘exit’
- Call
unref()for background processes - Use
execSynconly for short commands - Handle both stdout and stderr
- Set timeouts for long-running processes
- Clean up event listeners when done
- Use
spawnSyncfor testing/debugging - Check exit codes before using output
- Close stdin when done writing