0

I have a file that needs to export the output of a command performed by 'node-cmd' package. But module.exports cannot be run inside closure. How do i somehow set the closure output to global scope and use it? Simplified not working code example below.

const cmd = require('node-cmd');

let node_ver;

cmd.get('node -v', (err, data, stderr) => node_ver = data);

module.exports = node_ver;
Rastur
  • 137
  • 1
  • 3
  • 12

1 Answers1

1

The problem is that the module needs to return its exports immediately and cmd.get is asynchronous.

Export a Promise instead.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335