Gulp tasks can utilize environment variables through the ‘
process.env
’ object. To set an environment variable, use ‘
export VARNAME=value
’ in Unix or ‘
set VARNAME=value
’ in Windows before running Gulp. In your Gulpfile.js, access this with ‘
process.env.VARNAME
’.
For example, to create a task that behaves differently based on the NODE_ENV variable:
gulp.task('taskname', function() {
if(process.env.NODE_ENV === 'production') {
// Production behavior
} else {
// Development behavior
}
});​
Run it using ‘
NODE_ENV=production gulp taskname
’ for production and simply ‘
gulp taskname
’ for development. This allows you to customize tasks based on the environment.