exec child_process in a node js module

I'm createing a node module that I want to have a function called cpuUtilization that calls a python script and the stdout is returned.

var sys = require('sys')
var exec = require('child_process').exec;

exports.cpuUtilization = function() {
    exec('./nodeprime/cpuUtilization.py', function (error, stdout, stderr) {
        return(JSON.parse(stdout));
    });
}

You do net get a result back from cpuUtilizationbecause the return statement does not apply to that function, but to the callback inside that function.

You need to transform your code into asynchronous one and return the result using a callback as well:

exports.cpuUtilization = function (callback) {
  exec('./nodeprime/cpuUtilization.py', function (err, stdout, stderr) {
    if (err) return callback(err);
    callback(null, JSON.parse(stdout));
  });
};

It's common sense that callbacks in Node.js should return (at least) two parameters, where the first one is an error instance, and the second one contains the actual data.

Now you can use your function, e.g.:

cpuUtilization(function (err, data) {
  // Do something with data ...
});

Generally I'd recommend to you to make yourself familiar with higher-order functions.