我正在使用cmd来执行我的主厨命令,我如何使用node.js运行它们?
PS C:\Users\xyz\chef-repo> chef-apply script.rb
我想使用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();
};
这是我的代码。我收到此错误,我的命令是.bat文件。
{ [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"' }
答案 0 :(得分:1)
看看child_process.exec
function。所以你的电话会是这样的:
const exec = require('child_process').exec;
const child = exec('chef-apply script.rb',
(error, stdout, stderr) => {
# Your callback here
});
答案 1 :(得分:0)
var exec = require('child_process').exec;
exports.testscript=function(req,res){
//console.log("inside function");
var cmd = "chef-apply C:\\Users\\xyz\\chef-repo\\script.rb";
var child = exec(cmd + "," +
function(error, stdout, stderr){
console.log(child.stdout);
console.log(child.stderr);
if (error !== null) {
console.log(error);
}
});
child.stdin.end();
};
这是我的代码。我没有收到任何错误或输出。我想我错过了什么。