div*_*ivz 41 javascript node.js
我不知道如何执行exe
文件node.js
.这是我正在使用的代码.它不起作用,不打印任何东西.有没有办法exe
使用命令行执行文件?
var fun = function() {
console.log("rrrr");
exec('CALL hai.exe', function(err, data) {
console.log(err)
console.log(data.toString());
});
}
fun();
Run Code Online (Sandbox Code Playgroud)
Chi*_*ain 51
你可以在node.js中尝试子进程模块的execFile函数
参考:http: //nodejs.org/api/child_process.html#child_process_child_process_execfile_file_args_options_callback
您的代码应该类似于:
var exec = require('child_process').execFile;
var fun =function(){
console.log("fun() start");
exec('HelloJithin.exe', function(err, data) {
console.log(err)
console.log(data.toString());
});
}
fun();
Run Code Online (Sandbox Code Playgroud)
Bas*_*Joe 10
如果exe
您要执行的 是在其他目录中,并且您exe
对它所在的文件夹有一些依赖关系,请尝试cwd
在选项中设置参数
var exec = require('child_process').execFile;
/**
* Function to execute exe
* @param {string} fileName The name of the executable file to run.
* @param {string[]} params List of string arguments.
* @param {string} path Current working directory of the child process.
*/
function execute(fileName, params, path) {
let promise = new Promise((resolve, reject) => {
exec(fileName, params, { cwd: path }, (err, data) => {
if (err) reject(err);
else resolve(data);
});
});
return promise;
}
Run Code Online (Sandbox Code Playgroud)
如果您使用 Node.js 或任何支持 Node.js 的前端框架(React或Vue.js)
const { execFile } = require('child_process');
const child = execFile('chrome.exe', [], (error, stdout, stderr) => {
if (error) {
throw error;
}
console.log(stdout);
});
Run Code Online (Sandbox Code Playgroud)
如果.exe文件位于计算机中的某个位置,请将chrome.exe替换为您要执行的应用程序的路径,例如"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
const child = execFile('C:\Program Files (x86)\Google\Chrome\Application\chrome.exe', [], (error, stdout, stderr) => {
if (error) {
throw error;
}
console.log(stdout);
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
54644 次 |
最近记录: |