Executing an exe with parameters in nodejs

Ajith picture Ajith · Apr 6, 2016 · Viewed 11.4k times · Source

I wanted to execute an exe using node js. This is how the command looks in command prompt of windows:

oplrun -D VersionId=3458 -de "output.dat" "Test.mod" "Test.dat"

This runs fine and I get the output in output.dat file. Now, I wanted to execute the same with nodejs and I used execFile for this. It runs fine if I run:

var execFile = require('child_process').execFile;

execFile('oplrun',['Test.mod','Test.dat'], function(err, data) {
        if(err) {
            console.log(err)
        } 
        else 
        console.log(data.toString());                       
    }); 

However, if I wanted to pass the output file or version as parameter, it does not execute and I am not getting any error as well. Here is the code:

var execFile = require('child_process').execFile;

var path ='D:\\IBM\\ILOG\SAMPLE\\output.dat';

execFile('oplrun', ['-de',path],['Test.mod','Test.dat'], function(err, data) {
        if(err) {
            console.log(err)
        } 
        else 
        console.log(data.toString());                       
    }); 

How do I pass the parameters if I need pass something like -D VersionId=1111 or -de output.dat.

Thank you, Ajith

Answer

duncanhall picture duncanhall · Apr 6, 2016

The signature of execFile() is shown in the Node docs as:

file[, args][, options][, callback]

As you are not providing any options, you should be passing a single array, as in your first example.

execFile('oplrun', ['-de', 'output.dat', 'Test.mod','Test.dat'], function(err, data) {
        if(err) {
            console.log(err)
        } 
        else 
        console.log(data.toString());                       
    });