DEV Community

LG
LG

Posted on

Your doctor (parent process) – is your prescription publisher, meanwhile you, well guess who you are ?..

If you reading , it means you are subscriber , you (as child process) read doctor's stdout & stderr i.e. you are a child process (subscriber) that consumes the output as Writable, not as Readable stream as what you may expected initially. In layman's terms you behave opposite than you doctor, common sense, you are not a doctor (unless RIP Don Shirley)

const path = require('path')
const child_process = require('child_process');
// child_process.spawn(command@string, args@array, {options@object})

let ls = child_process.spawn('ls', ['-la'], {cwd: path.join(__dirname)})
// console.log(ls.pid /* self */ === ls._handle.pid /* inherited from parent */)

// the line below will be ignored , 'cause this is child process : it does not intended to consume stdin ;
ls.stdin.on('data', (data)=>console.log("data:", data))

ls.stdout.on('data', (data) => console.log(`stdout: ${data}`));

ls.stderr.on('data', (data) => console.log(`stderr: ${data}`)); // give spawn command as e.g lsapfhjsfjka to reproduce error

ls.on('close', (code) => console.log(`child process exited with code ${code}`));

Enter fullscreen mode Exit fullscreen mode

This been first of short series on Dev.to . See you in a next one !

Thanks to @devternity for support I've been spared up today !

Top comments (0)