run a windows batch file from node.js

前端 未结 4 1353
北恋
北恋 2020-12-05 15:58

am trying to run a test.bat file inside node.js

here is the code

var exec = require(\'child_process\').execFile;

case \'/start\':
    req.on(\'data         


        
4条回答
  •  囚心锁ツ
    2020-12-05 16:19

    In Windows, I don't prefer spawn as it creates a new cmd.exe and we have to pass the .bat or .cmd file as an argument. exec is a better option. Example below:

    Please note that in Windows you need to pass the path with double backslashes. E.g. C:\\path\\batfilename.bat

    const { exec } = require('child_process');
    exec("path", (err, stdout, stderr) => {
      if (err) {
        console.error(err);
        return;
      }
      console.log(stdout);
    });
    

提交回复
热议问题