0

I am using cmd to execute my chef commands, How do i run them using node.js?

PS C:\Users\xyz\chef-repo> chef-apply script.rb

I want to run this command using node.js

exports.testscript=function(req,res){
var exec = require('child_process').exec;
console.log("inside function");
 var child = exec('chef-apply azurepro.rb' ,{cwd: 'C:\Users\anurag.s\chef-repo'},
    function(error, stdout, stderr){
    console.log(stdout); 
    console.log(stderr); 
    if (error !== null) {
      console.log(error);
    }
        });

//child.stdin.end();
};

this is my code. I am getting this error and my command is .bat file.

{ [Error: spawn cmd.exe ENOENT]
 code: 'ENOENT',
 errno: 'ENOENT',
 syscall: 'spawn cmd.exe',
path: 'cmd.exe',
cmd: 'cmd.exe /s /c "chef-apply azurepro.rb"' }
Anurag
  • 73
  • 8
  • Possible duplicate of [node.js shell command execution](http://stackoverflow.com/questions/14458508/node-js-shell-command-execution) – StephenKing Mar 21 '16 at 11:04

1 Answers1

1

Take a look at child_process.exec function. So your call would be like this:

const exec = require('child_process').exec;
const child = exec('chef-apply script.rb',
  (error, stdout, stderr) => {
    # Your callback here
});
max
  • 2,757
  • 22
  • 19
  • There is no output after execution of the script. The script I use provisions and bootstraps a VM in azure. It runs fine both in powershell and bash but here there is neither response nor any error message. – Anurag Mar 21 '16 at 13:33
  • You would have to print out the stdout and/or stderr yourself. – coderanger Mar 21 '16 at 18:06