2

Is there a way to detect what version of Gulp is running (available to utilize in a gulpfile)?

I've got two separate gulpfile's I'm using amongst different environments, some that require v3 and some v4. For easier version control, I would prefer it if I could combine those files and not have to deal with different file names in different environments to eliminate confusion between multiple developers. Obviously to accomplish this I would need the script to differentiate between versions.

Talk Nerdy To Me
  • 626
  • 5
  • 21

2 Answers2

2

Alternatively to @alireza.salemian's solution, you could try to run the command line version command in javascript:

Depending on your JavaScript backend, your code may vary slightly, but inspired by this post you could run it as below:

    const execSync = require('child_process').execSync;
    // import { execSync } from 'child_process';  // replace ^ if using ES modules
    const output = execSync('gulp -v', { encoding: 'utf-8' });  // the default is 'buffer'
    const str_pos = output.search('Local version') + 14;
    const gulp_version = output.substring( str_pos, str_pos + 5 );
    console.log( 'Gulp version: ' + gulp_version );
avgJoe
  • 832
  • 7
  • 24
1

You can read package.json and find gulp version

const pkgJson = fs.readFileSync('./package.json', { encoding: 'utf8' });
const pkg = JSON.parse(pkgJson);
const gulpVersion = pkg['devDependencies']['gulp'];

It may not be the best solution, but you can quickly determine the gulp version.

alireza.salemian
  • 536
  • 5
  • 21