Search code examples
node.jscommand-line-interfacechild-process

Executing Command in Nodejs and Attaching the output to the console


I'm working on a CLI tool to add an extra layer of automation and utilities to our workflow and I'm wrapping the webpack development command with an alternative in my CLI (here's a demonstration):-

function runDev(){
   this.doSomePreAutomationAndPreperations();
   this.runWebpackDevCommand();
}

I'm using NodeJs child_proecess.exec and I'm trying to figure out a way to execute the webpack dev command and attach it to the terminal (like -it in docker if you're familiar with it) or transferring the control to the child process(so output will be directly emitted to the console).

Is there away to do that?


Solution

  • It turns out that I can achieve this but just making the child process inherit the stdio. ex:-

    const { spawn } = require('child_process')
    const shell = spawn('sh',[], { stdio: 'inherit' })
    shell.on('close',(code)=>{console.log('[shell] terminated :',code)})