DEV Community

LG
LG

Posted on

2 1

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)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay