简体   繁体   English

在 nodejs 中运行 bat 文件时出现空白 cmd 提示

[英]Getting a blank cmd prompt on running a bat file in nodejs

I am trying to run a.bat file by passing arguments to it in nodejs.我正在尝试通过在 nodejs 中将 arguments 传递给它来运行 .bat 文件。 However, I get an empty command prompt.但是,我得到一个空的命令提示符。 How to get the command pasted on the path.如何获取粘贴在路径上的命令。

code -代码 -

server.js服务器.js

var bat = require.resolve('E:/API_Gateway_through_Swagger/New_Project/aws-bat-file-3.bat');
var num = "123"
var state_desc = "abc";

var ls = spawn(bat, [num, state_desc  ]);

ls.stdout.on('data', function (data) {
    console.log('stdout: ' + data);
});

ls.stderr.on('data', function (data) {
    console.log('stderr: ' + data);
});

ls.on('exit', function (code) {
    console.log('child process exited with code ' + code);
});

Bat file -蝙蝠档案 -

aws-bat-file-3.bat aws-bat-file-3.bat

echo off
echo "Batch Started"
set arg1=%1
set arg2=%2

START "Task Options"  cmd.exe /b " cd C:\Users\myfolder &  aws apigateway create deployment  --rest-api-id  %arg1%  --stage-name dev  --stage-description  %arg2%"

output img file is - output img 文件是 -

命令提示符

NodeJS documented the problem of spawning batch files on Windows thoroughly in this section . NodeJS 在本节中彻底记录了在 Windows 上生成批处理文件的问题。

Excerpt:摘抄:

When running on Windows, .bat and.cmd files can be invoked using child_process.spawn() with the shell option set, with child_process.exec(), or by spawning cmd.exe and passing the.bat or.cmd file as an argument (which is what the shell option and child_process.exec() do). When running on Windows, .bat and.cmd files can be invoked using child_process.spawn() with the shell option set, with child_process.exec(), or by spawning cmd.exe and passing the.bat or.cmd file as an参数(这是 shell 选项和 child_process.exec() 所做的)。

 const bat = spawn('"my script.cmd"', ['a', 'b'], { shell: true });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM