Skip to content

Instantly share code, notes, and snippets.

@oze4
Created December 24, 2021 02:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oze4/6235640ca6cc59faecee9e275bff65d6 to your computer and use it in GitHub Desktop.
Save oze4/6235640ca6cc59faecee9e275bff65d6 to your computer and use it in GitHub Desktop.
child_process.spawn node.js
// https://stackoverflow.com/a/10232330/10431732
const childProcess = require('child_process');
// just runs `ls -a ../`
const childp = childProcess.spawn('ls', ['-a', '../']);
// assign stdout to variable
const variable = chilldp.stdout;
// listen for events on that variable
variable.on('data', (data) => {
console.log('stdout: ' + data.toString());
});
// You can listen for many events
childp.stderr.on('data', (data) => {
console.log('stderr: ' + data.toString());
});
childp.on('exit', (code) => {
console.log('child process exited with code ' + code.toString());
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment