Can't kill child process on Windows

后端 未结 4 1817
暗喜
暗喜 2021-01-11 20:24

The following will never exit

var child_process = require(\'child_process\');

var ps = child_process.spawn(\'.\\\\node_modules\\\\.bin\\\\babel.cmd\', [\'in         


        
4条回答
  •  逝去的感伤
    2021-01-11 21:14

    The most straight forward solution, don't spawn scripts instead spawn node directly.

    For unix, the script is the npm executable. For windows however we need to resolve the name from the .cmd file.

    if (path.extname(command).toLowerCase() == '.cmd') {
      var content = '' + fs.readFileSync(command);
      var link = (/node  "%~dp0\\(.*?)"/.exec(content) || [])[1];
    
      if (link) {
        command = path.resolve(path.dirname(command), link);
      }
    }
    
    var ps = child.spawn('node', [command].concat(args), options);
    

提交回复
热议问题