我需要在node.js函数

result = execSync('node -v');

它将同步执行给定的命令行并返回该命令文本的所有stdout。

ps.同步错误。我知道。仅供个人使用。

更新

现在我们有了mgutz的解决方案,它给出了退出代码,但没有stdout!还在等更确切的答案。

更新

Mgutz更新了他的答案,解决方案在这里:) 同样,作为dgo。一提到,有一个独立的模块execc -sync

更新2014-07-30

ShellJS lib到了。考虑到这是目前最好的选择。


更新2015-02-10

终于!NodeJS 0.12原生支持execSync。 查看官方文件


当前回答

这在Node.js中是不可能的,无论是child_process。Spawn和child_process。Exec是从底层构建为异步的。

详情见:https://github.com/ry/node/blob/master/lib/child_process.js

如果你真的想要这种阻塞,那么把所有需要发生的事情都放在回调中,或者构建自己的队列以阻塞的方式处理它,我想你可以使用Async.js来完成这个任务。

或者,如果你有太多的时间,可以在Node.js中自己动手。

其他回答

5年来,我的方法是有两条线;

const { execSync } = require('child_process');
const shell = (cmd) => execSync(cmd, {encoding: 'utf8'});

然后享受: Shell ('git remote -v') 或 Out = shell('ls -l') . .等等

你可以在nodejs中执行同步shell操作,如下所示:

var execSync = function(cmd) {

    var exec  = require('child_process').exec;
    var fs = require('fs');

    //for linux use ; instead of &&
    //execute your command followed by a simple echo 
    //to file to indicate process is finished
    exec(cmd + " > c:\\stdout.txt && echo done > c:\\sync.txt");

    while (true) {
        //consider a timeout option to prevent infinite loop
        //NOTE: this will max out your cpu too!
        try {
            var status = fs.readFileSync('c:\\sync.txt', 'utf8');

            if (status.trim() == "done") {
                var res = fs.readFileSync("c:\\stdout.txt", 'utf8');
                fs.unlinkSync("c:\\stdout.txt"); //cleanup temp files
                fs.unlinkSync("c:\\sync.txt");
                return res;
            }
        } catch(e) { } //readFileSync will fail until file exists
    }

};

//won't return anything, but will take 10 seconds to run
console.log(execSync("sleep 10")); 

//assuming there are a lot of files and subdirectories, 
//this too may take a while, use your own applicable file path
console.log(execSync("dir /s c:\\usr\\docs\\"));

编辑-本例适用于windows环境,如有必要可根据您自己的linux需求进行调整

我习惯于在回调函数的末尾实现“同步”的东西。不是很好,但是很有效。如果您需要实现一系列命令行执行,则需要将exec包装到某个命名函数中并递归调用它。 这个模式对我来说似乎很有用:

SeqOfExec(someParam);

function SeqOfExec(somepParam) {
    // some stuff
    // .....
    // .....

    var execStr = "yourExecString";
    child_proc.exec(execStr, function (error, stdout, stderr) {
        if (error != null) {
            if (stdout) {
                throw Error("Smth goes wrong" + error);
            } else {
                // consider that empty stdout causes
                // creation of error object
            }
        }
        // some stuff
        // .....
        // .....

        // you also need some flag which will signal that you 
        // need to end loop
        if (someFlag ) {
            // your synch stuff after all execs
            // here
            // .....
        } else {
            SeqOfExec(someAnotherParam);
        }
    });
};

使用ShellJS模块。

不提供回调函数的Exec函数。

例子:

var version = exec('node -v').output;

这是我发现的最简单的方法:

exec-Sync: https://github.com/jeremyfa/node-exec-sync (不要与execSync混淆。) 同步执行shell命令。在迁移脚本、cli程序中使用这个选项,但不能用于常规的服务器代码。 例子:

var execSync = require('exec-sync');   
var user = execSync('echo $USER');
console.log(user);