javascript 如何在 Windows 上的 node.js 中运行外部程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11675716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 13:58:35  来源:igfitidea点击:

How do you run an external program in node.js on windows?

javascriptnode.js

提问by Joel

I'm creating an automated web app build process with node.js on windows. I'm trying to run our code through Google closure java program. I read the documentation on child_process in the node documents. It mentions it doesnt work in windows yet. Is there a package or work around for this?

我正在 Windows 上使用 node.js 创建一个自动化的 Web 应用程序构建过程。我正在尝试通过 Google 关闭 Java 程序运行我们的代码。我阅读了节点文档中关于 child_process 的文档。它提到它在 Windows 中不起作用。有没有包或解决这个问题?

Heres the code im trying to run.

这是我试图运行的代码。

var _exec = require('child_process').exec;
_exec( 'java ' + '-jar '+ COMPILER_JAR +' --js '+ srcPath +' --js_output_file '+ distPath, 
    function(e){
        echo( "google closure done....");
        echo( e );
    } );

回答by miltonb

I have a web server app for controlling a queue of builds on windows XP and I used it to run batch files or executables without any additional packages.

我有一个 Web 服务器应用程序,用于控制 Windows XP 上的构建队列,我用它来运行批处理文件或可执行文件,而无需任何其他包。

I would check the error parameter on the callback and stderr as this may help you find the reason it does not work.

我会检查回调和 stderr 上的错误参数,因为这可能会帮助您找到它不起作用的原因。

My example solution out of my server which I hope helps:

我的服务器上的示例解决方案希望能有所帮助:

var theJobType = 'FOO';
var exec = require('child_process').exec;
var child = exec('Test.exe ' + theJobType, function( error, stdout, stderr) 
   {
       if ( error != null ) {
            console.log(stderr);
            // error handling & exit
       }

       // normal 

   });