I'm setting up a new application for my final project. I'm trying to execute an exe file using node.js, but it looks that it's not working right.
I don't get any errors...
var {execFile} = require('child_process');
var executablePath = "C:\\Users\\MyUser\\Desktop\\finalProject\\routes\\api\\testcpp.exe";
const child = execFile(executablePath, ['--version'], (error, stdout, stderr) => {
if (error) {
throw error;
}
console.log(stdout);
});
var {execFile} = require('child_process');
var executablePath = "C:/Users/MyUser/Desktop/finalProject/routes/api/testcpp.exe";
const child = execFile(executablePath, ['--version'], (error, stdout, stderr) => {
if (error) {
throw error;
}
console.log(stdout);
});
Related
var exec = require('child process').execFile;
exec('C:\something.exe', function (err, data) {
console.log(err)
console.log(data.toString());
});
You can accomplish this by using the exec function from the built-in child_process module as shown in the NodeJS documentation.
const { exec } = require('child_process');
exec('start program.exe', (error, stdout, stderr) => {
if (error) {
throw error;
}
console.log(stdout);
});
I want to execute the command which takes two arguments. I tried the below step but it is not working. Please help
const exec = require('child_process').exec;
var inputfile = "./uploads/EM_spectrum.ppt";
var outfile = "./uploads/EM_spectrum.ppt.pdf"
exec('./px-8-5-4-win-x86-64/sdk/demo/pxsample.exe', [inputfile, outfile]);
Use the below code
const exec = require('child_process').exec;
var inputfile = "./uploads/EM_spectrum.ppt";
var outfile = "./uploads/EM_spectrum.ppt.pdf";
exec(`./px-8-5-4-win-x86-64/sdk/demo/pxsample.exec ${inputfile} ${outfile}``, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(`stdout: ${stdout}`);
console.error(`stderr: ${stderr}`);
});
I am writing some code that lists some functionalities which are run by cmd, How can I enclose cmd executables in my code?
Here is a small example to use child_process.
Execute command in cmd.
const { exec } = require("child_process");
function os_func() {
this.execCommand = function(cmd, callback) {
exec(cmd, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
callback(stdout);
});
}
}
app.get("/", (req, res) => {
console.log("inside get");
var os = new os_func();
os.execCommand('arp -a', function (returnvalue) {
res.end(returnvalue)
});
});
There is a global function i.e:
process.argv
Or if you want to take command-line arguments a lot better try npm package yargs.
I like to integrate exec from nodejs in a custom function to handle all the errors in this one function.
const exec = require('child_process').exec;
function os_func() {
this.execCommand = function(cmd) {
var ret;
exec(cmd, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
ret = stdout;
});
return ret;
}
}
var os = new os_func();
This function returns undefined because exec isn't finished when the value returns.
How can i solve that? Can i force the function to wait for exec?
you can use promise as :
const exec = require('child_process').exec;
function os_func() {
this.execCommand = function (cmd) {
return new Promise((resolve, reject)=> {
exec(cmd, (error, stdout, stderr) => {
if (error) {
reject(error);
return;
}
resolve(stdout)
});
})
}
}
var os = new os_func();
os.execCommand('pwd').then(res=> {
console.log("os >>>", res);
}).catch(err=> {
console.log("os >>>", err);
})
Since the command is executed asynchronously you will want to use a callback to handle the return value once the command has finished executing:
const exec = require('child_process').exec;
function os_func() {
this.execCommand = function(cmd, callback) {
exec(cmd, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
callback(stdout);
});
}
}
var os = new os_func();
os.execCommand('SomeCommand', function (returnvalue) {
// Here you can get the return value
});
Yet another solution using ES6 modules:
import fs from "node:fs";
import {exec} from "node:child_process";
import util from "node:util";
// promisify exec
const execPromise = util.promisify(exec);
try {
// wait for exec to complete
const {stdout, stderr} = await execPromise("ls -l");
} catch (error) {
console.log(error);
}
exec will deal with it in an async fashion, so you should receive a callback or return a promise.
One thing you could do in order to make it sync is to use execSync instead:
https://nodejs.org/api/child_process.html#child_process_child_process_execsync_command_options
The child_process.execSync() method is generally identical to
child_process.exec() with the exception that the method will not
return until the child process has fully closed. When a timeout has
been encountered and killSignal is sent, the method won't return until
the process has completely exited. Note that if the child process
intercepts and handles the SIGTERM signal and doesn't exit, the parent
process will wait until the child process has exited.
Adding what worked for me, as none of the above did the trick!
const { exec } = require("child_process");
const util = require("util");
const execPromise = util.promisify(exec);
function parentFunction() {
...
// Trigger 'exec', then a-wait for it to finish
await execWrapper('<your-command-here>');
...
}
...
async function execWrapper(cmd) {
const { stdout, stderr } = await execPromise(cmd);
if (stdout) {
console.log(`stderr: ${stdout}`);
}
if (stderr) {
console.log(`stderr: ${stderr}`);
}
}
NOTE: This isn't your example, but just a generic one; for me - the cmd was a Docker build command. You could probably have execWrapper return back the stdout if needed.
You can do it with callback. Maybe you can try something like this:
function os_func() {
this.execCommand = function(cmd, myCallback) {
var ret;
exec(cmd, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
ret = stdout;
myCallback(ret);
});
}
function myCallback(ret){
// TODO: your stuff with return value...
}
I have to run file test.js which is at different location than the my running application. To do that i have tried the fallowing code
var execFile = require("child_process").execFile;
exports.sync = function(req, res) {
console.log("sync called");
var child = execFile("C:/Users/rhush/Desktop/test", function(error, stdout, stderr) {
if (error) {
throw error;
}
console.log(stdout);
res.send({ status: stdout });
});
};
and my test file is here :
function testing() {
console.log('sync job running');
}
testing();
but i got the error
please correct if i am doing any mistake.
To run a js file using execFile you need to pass node command with file name, Use this one:
var execFile = require("child_process").execFile;
exports.sync = function(req, res) {
console.log("sync called");
var child = execFile("node", ["C:/Users/rhush/Desktop/test.js"], function(error, stdout, stderr) {
if (error) {
throw error;
}
res.send({ status: stdout });
});
};