0

How do I assign a value to a variable using the command line when using a gulp task.

gulp.task('task-name',function(value){
   var anotherValue = value;
});

Something like this, so when I run gulp task-name value I can access value.

I have tried using gulp-param, but that prevents some of my tasks from running.

Kylar
  • 424
  • 11
  • 16
  • Possible duplicate of [Is it possible to pass a flag to Gulp to have it run tasks in different ways?](http://stackoverflow.com/questions/23023650/is-it-possible-to-pass-a-flag-to-gulp-to-have-it-run-tasks-in-different-ways) – Sven Schoenung Sep 14 '16 at 05:10

2 Answers2

0

easiest solution would be to set the env vars before calling the gulp and using them via process.env.MYVAR.

For example:

MY_VAR=12345
gulp

And in the gulp task:

gulp.task('task-name', function () {
  var anotherValue = process.env.MY_VAR;
}
sagie
  • 1,744
  • 14
  • 15
0

You can pass values into a gulp task via the command line. The easiest way to do this is to use an args module like yargs.

var args = require('yargs').argv;

// this isn't strictly necessary but it's good to set default values
var target = args.target || 'default';

gulp.task('something', function() {
    gulp.src('*.html')
        // let's use our variable here
        .pipe(dest(target));
});

Then call this from the command line:

gulp something --target blah

Where blah will be the value of target inside the gulp task.

Soviut
  • 88,194
  • 49
  • 192
  • 260