require('child_process').spawn();
- large data, stream, NOT a new V8 instancerequire('child_process').fork();
- new V8 instance, multiple workersrequire('child_process').exec();
- buffer, async, all the data at once
const { spawn } = require('child_process');
spawn('node', 'program.js').stdout.on('data', data => {
console.log('stdout: ' + data);
});
const { fork } = require('child_process');
fork('program.js').stdout.on('data', data => {
console.log('stdout: ' + data);
});
const { exec } = require('child_process');
exec('node program.js', (error, stdout, stderr) => {
if (error) console.log(error.code);
});
execFile()
- is similar toexec()
except that it does not spawn a shell by default. Rather, the specified executable file is spawned directly as a new process making it slightly more efficient thanexec()
And sync versions of exec
and execFile
execSync()
execFileSync()