ghostScript with Node generates fatal error

I'm using ghostscript with Node (running on MacOX and Ubuntu). my ghostscript command is this:

/usr/local/bin/gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=output.pdf -dBATCH input1.pdf input2.pdf 

When I run this command manually, it works. When it's kicked off by NodeJS, I get this error:

[2014-09-08 10:33:20.587] [INFO] console - { [Error: Command failed: GPL Ghostscript 9.10: Unrecoverable error, exit code 1
] killed: false, code: 1, signal: null }
Error: Command failed: GPL Ghostscript 9.10: Unrecoverable error, exit code 1

at ChildProcess.exithandler (child_process.js:647:15)
at ChildProcess.EventEmitter.emit (events.js:98:17)
at maybeClose (child_process.js:753:16)
at Socket.<anonymous> (child_process.js:966:11)
at Socket.EventEmitter.emit (events.js:95:17)
at Pipe.close (net.js:465:12)

Node code looks like this:

var execCommand = config.ghostScript + " -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=" + finalFileName + " -dBATCH " + inputFiles;
exec(execCommand, function (error, stdout, stderr) {
    if ( error !== null ) {
        var err = new Error("pdf could not be generated");
        err.status = constants.HTTPCODE_INTERNAL_SERVER_ERROR;
        throw err;
    } else {
        console.log("success");
    }

Any ideas?

I suspect that user1283633 is correct. In any event you should first print out what's in your 'exec' variable and see if its what you expect. 9 times out of 10 when people say 'this command line works, but when I put it in my program it doesn't' it turns out that what's being generated by their application isn't the same command line.

You should also capture stdout and stderr from Ghostscript (if nothing else you can redirect them to files). Ghostscript is undoubtedly printing more information about the problem, and you are simply discarding it.