You need to send a message to the process that is running through spawn.

const prcs = spawn('node', [ path_, `--url=${url}`, ], {}); prcs.stdout.on('data', (stdout) => { prcs.send({ prop1: 100 }); }) 

But an error is displayed

prcs.send ({

TypeError: prcs.send is not a function

    1 answer 1

    To be able to send messages you must use fork instead of spawn

    The child_process.fork () method is used specifically for spawn new Node.js processes. Like child_process.spawn (), a ChildProcess object is returned. The child process will be a communication channel. See subprocess.send () for details.

    So in the end it will be like this:

     // master.js 'use strict'; const path = require('path'); const { fork } = require('child_process'); const prcs = fork(path.join(__dirname, './slave.js')); prcs.on('message', msg => { console.log('SLAVE is saying:', msg); }); prcs.send('DO IT!!!'); // slave.js 'use strict'; process.on('message', msg => { console.log('PARENT is saying:', msg); }); setTimeout(() => { process.send('Let me free please'); process.exit(0); }, 1000); 
    • And for spawn, how can this be done? - manking
    • judging by the documentation - in any way, but in general it all depends on what you want to do, if you simply pass some parameter through env but bidirectional communication cannot be organized so - D.Dimitrioglo
    • I need to complete the child process. He transferred the data, I received it and then I need to complete this process. That's right? myChildProcess.stdin.pause (); myChildProcess.kill (); - manking
    • copy my code snippet and check, but as far as I remember you have to send some message from the parent and only with this message the fork can "self - terminate " - D.Dimitrioglo
    • No, not with fork, but with spawn. When fork is used because the same executable node.js as the parent? - manking