Techdive
Techdive

Reputation: 1043

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. However, I get an empty command prompt. How to get the command pasted on the path.

code -

server.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

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 -

cmd-prompt

Upvotes: 0

Views: 567

Answers (1)

SOFe
SOFe

Reputation: 8214

NodeJS documented the problem of spawning batch files on Windows thoroughly in this section.

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).

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

Upvotes: 1

Related Questions