node-cross-spawn icon indicating copy to clipboard operation
node-cross-spawn copied to clipboard

In Windows, spawn.kill() doesn't kill child process

Open localhosted opened this issue 4 years ago • 0 comments

Trying to run a project that uses cross-spawn and in Windows spawn() spawns a cmd that the runs my command. When killing the spawned pid the node process doesn't die:

let node;

compiler.hooks.watchRun.tap("Dev", (compiler) => {
  console.log(`Compiling ${compiler.name} ...`);
  if (compiler.name === "server" && node) {
    // I added this to try and force kill the cmd instance, but it won't kill the child node process
    if (isWin) {
      spawn("taskkill", ["/pid", node.pid, "/F", "/T"]);
    }
    const r = node.kill();
    node = undefined;
  }
});

compiler.watch({}, (err, stats) => {
  if (err) {
    console.error(err);
    process.exit(1);
  }
  console.log(stats?.toString("minimal"));
  const compiledSuccessfully = !stats?.hasErrors();
  if (compiledSuccessfully && !node) {
    console.log("Starting Node.js ...");
    node = spawn(
      "node",
      ["--inspect", path.join(__dirname, "dist/server.js")],
      {
        stdio: "inherit",
      }
    );
  }
});

Switching to child_process works fine

localhosted avatar Dec 01 '21 01:12 localhosted