Search code examples
node.jswindowsubuntunpmgulp

NodeJS exec() command for both Windows and Ubuntu


Using NodeJS, NPM, and Gulp.

I want to build a gulp task to run JSDoc that works on Ubuntu and Windows.

This works on Ubuntu...

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

return function(cb) {
  exec('node node_modules/.bin/jsdoc -c jsdoc-conf.json', function(err, stdout, stderr) {
    cb(err);
  });
};

And this works on Windows...

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

return function(cb) {
  exec('node_modules\\.bin\\jsdoc -c jsdoc-conf.json', function(err, stdout, stderr) {
    cb(err);
  });
};

Needless to say, neither works on the other. How do others solve this type of problem?


Solution

  • Node has process.platform, which... "returns a string identifying the operating system platform on which the Node.js process is running. For instance darwin, freebsd, linux, sunos or win32"

    https://nodejs.org/api/process.html#process_process_platform

    var exec = require('child_process').exec;
    
    return function(cb) {
      if (process.platform === 'win32') {
        // Windows OS
      } else {
        // everything else
      }
    };