node.jsbashshellcommand-line-interfaceinquirerjs

How can I start an interactive bash script from a node app?


I'm building a command-line app in Node which is basically a setup wizard. At some point in the script, I'd like to start an openssl script which has its own stdout and prompts, and then ideally return to my Node app when it's done.

I've tried using spawn but am having trouble showing the stdout. Is this practically possible?


Solution

  • Yes, this is possible, you can pipe standard input and output to your parent process like this,

    // Child will use parent's stdios
    spawn('yourcommand', [], { stdio: 'inherit' });
    

    first parameter is your command, second parameter is command parameters and third parameters is how to handle standard inputs and outputs,

    Refer this documentation for more information, https://nodejs.org/api/child_process.html#child_process_options_stdio