Passing arguments from the console to Gulp

Node scripts you need to pass arguments, getting them can be cumbersome.

If you want to run a task with certain parts turned off or on, or even pass a value from the command line to gulp, by default you can’t. Well you can, but it’s a node thing: process.argv it is just an array, so it’s a bit limited, so there is a module to abstract the mess: yargs .

npm install —save-dev yargs

Then you require it.

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

Now you have access to the arguments or flags passes from the console. Something like:

gulp css —verbose

will be available in the gulpfile as argv.verbose, or

gulp html —lang=es

will be reachable in the gulpfile with argv.lang and you’ll get the string.

no dependencies option

For simple flags that are either there or not you can just filter for them, without the need to install a package.

gulp css --no-minification

can be checked inthe script as follows

const = noMinification = process.argv.filter(item => item === 'no-minification');

…and that’s all, it does not scale well for more configuration flags, and options, but is cleaner than adding a dependency.

Published :

Last modified :


comments powered by Disqus