Node.js Process & Child Process
Why Stack Overflow recommends it
Stack Overflow users link it when answering questions about spawning child processes in Node and passing data between parent and child.
Recommended by 2 people across 2 different months, 2017 to 2021.
What it is
A 17-minute Node.js screencast by Kyle Robinson Young covering the process object and the child_process module, including how parent and child processes communicate. It is part of his Let's Write Code series and links to example code on GitHub.
What people said
Recommends chitzui · 2017-06 · Stack Overflow · on “How to stop webdriver without crashing node js”
Ok, I got this working. It’s a bit difficult solution but it works: Using Child Processes Basically, every time the app gets a get request to /automate it will now create a child process in node which runs the selenium scripts (a child process is kind of like using another thread. Here is a very good tutorial on child processes [this video] ): index.js "use strict"; const Express = require('express'); const { spawn } = require('child_process'); const data = require('./data.json'); const app = new Express(); app.get("/automate", (req, res) => { const child = spawn( process.execPath, [`${__dirname}/test.js`, JSON.stringify(data)], { stdio: ['inherit', 'inherit', 'inherit', 'pipe'] } ); child.stdio[
Recommends Jona Dev · 2018-02 · Stack Overflow · on “Pass REST API data back to Node command line”
Answer, Node Child Process! [LINK] I also found this video helpful: [this video] const exec = require('child_process').exec; exec(`pkill -9 -f ${mode}`, (err, stdout, stderr) => { if (err) { console.log(`There was an error killing mode ${mode}`); console.error('error: ', err); return; } console.log('kill restart! stdout: ', mode, stdout) console.log('kill restart! stderr: ', mode, stderr) });
Cites user15573857 · 2021-11 · Stack Overflow · on “communicating with a spawn() child process?”
I'm trying to create a child process using spawn() with it's own terminal parent.js: const spawn = require('child_process').spawn; console.log('started parent process...'); //this should be printed in the parent terminal const child = spawn('start node', [`child.js`], { cwd:__dirname, shell: true, stdio: [null, null, null, 'pipe'] }); const Name = 'general kenobi'; child.stdio[3].write(Name); child.stdio[3].on('data', (data) => { console.log('data=>', data.toString()); child.kill(); }); child.js: console.log('started child process...'); (async()=>{ await new Promise(r=>setTimeout(r,3000)); try{ let net = require('net'); let pipe = new net.Socket({ fd: 3 }); pipe.on('data',(data)=>{ pipe.writ