node-webkit

nw.js - Spawn child_process using nw.js' built-in Node?


What is the process for spawning a cli through nw.js' own Node? I don't want my users to have to have Node.js installed.

I've tried:

const {spawn} = require("child_process");
const child = spawn(process.execPath, ['node_modules/newman/bin/newman.js', 'run'], {
    shell: true,
    stdio: ['inherit']
});

This gives me the error "newman '"node"' is not recognized as an internal or external command, operable program or batch file."

Context:

I have built a nw.js app that acts as a GUI for a cli tool (newman).

Current behavior: When a button is clicked, child_process spawns a command, ex. "newman run". Current code snippet:

const {spawn} = require("child_process");
const child = spawn('newman run' {
    shell: true,
    stdio: ['inherit']
});

This does everything I want... except only as long as Node.js is installed on my machine. I would like to use nw.js' own Node functionality instead. Ex.:

C:\nw\nw.exe

C:\nw\node_modules\newman\bin\newman.js

What adjustments would I need to do to spawn this cli through nw.js' own Node?


Solution

  • NW.js is not really meant for launching separate Node.js scripts. You cannot use child_process to launch a new Node.js script using nw.exe.

    What you can do is read in the script, then use Node's VM module to load the script you want to run: https://nodejs.org/api/vm.html

    All that said, you really don't need all that to use the newman package. Run it as a library, not as a CLI command: https://www.npmjs.com/package/newman#using-newman-as-a-library