prashanth prashanth - 4 years ago 141
PHP Question

How to get existing php/python version number from node.js app

I was trying to create a command line node.js app using commander module. This app would download few files based on php version installed in my machine. Can someone help me with sample of how to get the php version number from node.js app

I tried something as below but dint worked, i would replace npm -v with php -v later

var exec = require('child_process').execFile;
exec('npm -v', function(err, data) {
console.log(err)
console.log(data.toString());
});


Error:

{ [Error: spawn npm -v ENOENT]
code: 'ENOENT',
errno: 'ENOENT',
syscall: 'spawn npm -v',
path: 'npm -v',
cmd: 'npm -v' }

Answer Source

child_process.exec() works fine for me:

var exec = require('child_process').exec;
exec('php -r "echo phpversion();"', function(err, stdout, stderr) {
  console.dir(stdout.toString());
});

// Outputs:
// '5.6.11-1ubuntu3.1'

For python you can do something like this:

var exec = require('child_process').exec;
exec('python -c "import platform; print(platform.python_version())"',
     function(err, stdout, stderr) {
  console.dir(stdout.toString());
});

// Outputs:
// '2.7.10'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download