How to execute an external program from within Node js

0 votes
Is it possible to execute an external program from within node.js? Is there an equivalent to Python's os.system() or any library that adds this functionality?
Jul 17, 2020 in Node-js by kartik
• 37,490 points
1,044 views

1 answer to this question.

0 votes

Hello @kartik,

Exec has memory limitation of buffer size of 512k. In this case it is better to use spawn. With spawn one has access to stdout of executed command at run time

var spawn = require('child_process').spawn;
var prc = spawn('java',  ['-jar', '-Xmx512M', '-Dfile.encoding=utf8', 'script/importlistings.jar']);

//noinspection JSUnresolvedFunction
prc.stdout.setEncoding('utf8');
prc.stdout.on('data', function (data) {
    var str = data.toString()
    var lines = str.split(/(\r?\n)/g);
    console.log(lines.join(""));
});

prc.on('close', function (code) {
    console.log('process exit code ' + code);
});

Hope this is helpful!!
Thank you!!

answered Jul 17, 2020 by Niroj
• 82,680 points

Related Questions In Node-js

+1 vote
1 answer

How to make external HTTP requests with Node.js ?

Hello @kartik, Use this: var http = require('http'); var options ...READ MORE

answered Oct 11, 2020 in Node-js by Niroj
• 82,680 points
84 views
0 votes
1 answer

How to “Ping” from a Node.js app?

Hello @kartik, You could use exec to call the system ...READ MORE

answered Oct 16, 2020 in Node-js by Niroj
• 82,680 points
1,135 views
0 votes
1 answer

How to find the version of an installed node.js/npm package?

Hello, npm view <package> version - returns the latest ...READ MORE

answered Nov 24, 2020 in Node-js by Niroj
• 82,680 points
467 views
0 votes
1 answer

How to change to an older version of Node.js?

Hello, Install specific version of node sudo npm cache ...READ MORE

answered Nov 24, 2020 in Node-js by Niroj
• 82,680 points
97 views
0 votes
1 answer

How to write files in Node.js?

Hello @kartik, Currently there are three ways to ...READ MORE

answered Jul 8, 2020 in Node-js by Niroj
• 82,680 points
172 views
0 votes
1 answer

How to install a previous exact version of a NPM package?

Hello @kartik, If you have to install an ...READ MORE

answered Jul 8, 2020 in Node-js by Niroj
• 82,680 points
773 views
0 votes
1 answer

How to Install a local module using npm?

Hello @kartik, This is what worked for me: npm ...READ MORE

answered Jul 9, 2020 in Node-js by Niroj
• 82,680 points
374 views
0 votes
1 answer

How to run Gulp tasks sequentially one after the other?

Hello @kartik, By default, gulp runs tasks simultaneously, ...READ MORE

answered Jul 12, 2020 in Node-js by Niroj
• 82,680 points
240 views
0 votes
1 answer

How to stop node.js program from command line?

Hello @kartik, Ctrl+Z suspends it, which means it can ...READ MORE

answered Jul 15, 2020 in Node-js by Niroj
• 82,680 points
4,292 views
0 votes
1 answer

How to create an HTTPS server in Node.js?

Hello @kartik, The minimal setup for an HTTPS ...READ MORE

answered Jul 13, 2020 in Node-js by Niroj
• 82,680 points
129 views