4

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

Ajith
  • 83
  • 1
  • 1
  • 6
  • you can all the parameter in same array as comma separated. `['-de',path, 'Test.mod','Test.dat']` – Deendayal Garg Apr 06 '16 at 11:19
  • Getting the below error: execFile('oplrun', ['-de' path, 'Test.mod','Test.dat'], function(err, data) { ^^^^ SyntaxError: Unexpected identifier at Module._compile (module.js:439:25) at Object.Module._extensions..js (module.js:474:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) at Function.Module.runMain (module.js:497:10) at startup (node.js:119:16) at node.js:906:3 – Ajith Apr 06 '16 at 11:35

1 Answers1

6

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());                       
    }); 
duncanhall
  • 11,035
  • 5
  • 54
  • 86
  • Hi, I have tried that. But that doesn't work. Here is the error: { [Error: Command failed: Unknown option: -de output.dat ] killed: false, code: 10, signal: null } – Ajith Apr 06 '16 at 11:30
  • Thanks a lot Duncan. That's working fine. Appreciate your help. – Ajith Apr 06 '16 at 11:42