Anurag
Anurag

Reputation: 73

How to execute "chef-apply script.rb" command in node.js function?

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"' }

Upvotes: 0

Views: 140

Answers (1)

max
max

Reputation: 2817

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
});

Upvotes: 1

Related Questions