如何从node.js中执行一个传递一些参数的.bat文件
How to execute a .bat file from node.js passing some parameters?
我使用node.js v4.4.4,需要从node.js.运行一个.bat
文件
从我的节点应用程序的js文件的位置,.bat可以使用命令行运行,路径如下(窗口平台):
'../src/util/buildscripts/build.bat --profile ../profiles/app.profile.js'
但当使用node时,我无法运行它,不会抛出任何特定的错误。
我在这里做错了什么?
var ls = spawn('cmd.exe', ['../src/util/buildscripts', 'build.bat', '--profile ../profiles/app.profile.js']);
ls.stdout.on('data', function (data) {
console.log('stdout: ' + data);
});
ls.stderr.on('data', function (data) {
console.log('stderr: ' + data);
});
ls.on('exit', function (code) {
console.log('child process exited with code ' + code);
});
您应该能够运行这样的命令:
var child_process = require('child_process');
child_process.exec('path_to_your_executables', function(error, stdout, stderr) {
console.log(stdout);
});
下面的脚本解决了我的问题,基本上我必须:
-
转换为.bat文件的绝对路径引用。
-
使用数组将参数传递给.bat。
var bat = require.resolve('../src/util/buildscripts/build.bat'); var profile = require.resolve('../profiles/app.profile.js'); var ls = spawn(bat, ['--profile', profile]); ls.stdout.on('data', function (data) { console.log('stdout: ' + data); }); ls.stderr.on('data', function (data) { console.log('stderr: ' + data); }); ls.on('exit', function (code) { console.log('child process exited with code ' + code); });
以下是有用的相关文章列表:
https://nodejs.org/api/child_process.html#child_process_asynchronous_process_creation
https://nodejs.org/api/child_process.html#child_process_spawning_bat_and_cmd_files_on_windows
http://www.informit.com/articles/article.aspx?p=2266928
相关文章:
- JS:检查URL中的参数,然后迭代一个参数为var的函数
- 为什么jQuery文件的函数中有两个参数,但只接收一个参数
- 如何将一个*参数*从JS传递到AS3
- 如何在mysql查询(NODE.js)中重复使用一个参数
- Meteor.js只更新一个参数,而不是整个集合
- 必应地图没有'当我向函数添加另一个参数时,t显示
- Function.prototype.call和Function.protoype.all只应用一个参数
- Javascript/jQuery获取所有url参数并添加/更改一个参数
- 将一个参数传递给另一个参数
- 我需要将一个参数从另一个任务传递给gullow任务,或者用runSequence调用的函数替换一个任务
- 将多字段html输入组合为一个参数并传递给javascript
- jQuery使用最后一个参数在Javascript循环中单击绑定函数
- 如果我传递一个参数,则不会调用javascript函数
- Javascript:在URL后面添加一个参数
- pass函数是JavaScript中的一个参数
- 在backbone.js中路由视图时传递一个参数
- bookmarklet在url中添加一个参数并重新提交
- 在nodejs中使用一个参数
- 通过删除一个参数及其值来清理 URL
- 使用 onClick 从 JavaScript 创建 html 按钮调用一个带有一个参数的方法