child_process


1. Spawning a Child Process

const { spawn } = require('child_process');

const child = spawn('ls', ['-la']);

// Handle child process output
child.stdout.on('data', (data) => console.log(`stdout: ${data}`));
child.stderr.on('data', (data) => console.error(`stderr: ${data}`));

// Handle child process exit
child.on('close', (code) => code === 0 ? console.log('Child process exited successfully') : console.error(`Child process exited with code ${code}`));

2. Executing a Command Synchronously

const { execSync } = require('child_process');

try {
  const output = execSync('ls -la').toString();
  console.log(`stdout: ${output}`);
} catch (err) {
  console.error(`stderr: ${err.stderr}`);
}

3. Executing a Command Asynchronously

4. Spawning a Child Process with Input and Output Pipes

5. Sending Signals to a Child Process

6. Spawning a Child Process with Environment Variables

7. Spawning a Child Process with Working Directory

8. Piping Child Process Output to Another Process

9. Handling Child Process Events

10. Killing a Child Process

11. Waiting for Child Process to Exit

12. Executing a Command with Arguments

13. Executing a Command with Options

14. Executing a Command with Input

15. Executing a Command with Environment Variables

16. Executing a Command with Working Directory

17. Executing a Command with Shell

18. Executing a Command with Stdio

19. Executing a Command with Timeout