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,520 points
4,075 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!!

To know more about it, enroll in Node.js training today.

Thank You!!

answered Jul 17, 2020 by Niroj
• 82,840 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 12, 2020 in Node-js by Niroj
• 82,840 points
1,835 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,840 points
4,738 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,840 points
6,698 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,840 points
1,242 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,840 points
908 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,840 points
2,796 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,840 points
9,794 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,840 points
1,484 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,840 points
20,098 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,840 points
1,877 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP