Launch an external application from node.js

Solution 1:

you can do this

var cp = require("child_process");
cp.exec("document.docx"); // notice this without a callback..
process.exit(0); // exit this nodejs process

it not safe thought, to ensure that the command show no errors or any undesired output

you should add the callback parameter child_process.exec(cmd,function(error,stdout,stderr){})

and next you can work with events so you won't block execution of script or even make use of a external node.js script that launches and handles outputs from processes which you spawn from a "master" script.

Solution 2:

In below example I have used textmate "mate" command to edit file hello.js, you can run any command with child_process.exec but the application you want to open file in should provide you with command line options.

var exec = require('child_process').exec;
exec('mate hello.js');

Solution 3:

var childProcess = require('child_process');
childProcess.exec('start Example.xlsx', function (err, stdout, stderr) {
        if (err) {
        console.error(err);
        return;
    }
    console.log(stdout);
    process.exit(0);// exit process once it is opened
})

Emphasis on where 'exit' is called. This executes properly in windows.