Rund grunt task from node and keep result in variable

I would like to run a grunt task from a node .js file, sync.

Its a build process so all scripts/tasks that fail should halt the build.

I tried just

var res = grunt.tasks(['compile']);
console.log('res: ', res);

and that runs my compile task but nothing is stored in res. Actually i see that the task is ran async since the log appears before the task even started.

How can I do this?

btw, tried also

grunt.util.spawn({
    grunt: true,
    args: ['compile']
}, function(err, res, code) {
    console.log('err', err);
});

but that didn't give any output, just blocked my command line...

// If specified, the same grunt bin that is currently running will be // spawned as the child command, instead of the "cmd" option.

What I read here is that the spawn snippet is supposed to be run with grunt, not with node directly, as the question 's title implies.