How can I set an environment variable as gulp task?

gulp.task('set-dev-node-env', function() {
    return process.env.NODE_ENV = 'development';
});

gulp.task('set-prod-node-env', function() {
    return process.env.NODE_ENV = 'production';
});

Use it like:

gulp.task('build_for_prod', ['set-prod-node-env'], function() {
    // maybe here manipulate config object  
    config.paths.src.scripts = config.paths.deploy.scripts;
    runSequence(
        'build',
        's3'
    );
});

You can also define it as a script in your package.json

{
  "name": "myapp",
  "scripts": {
    "gulp": "NODE_ENV='development' gulp",
    "gulp-build": "NODE_ENV='production' gulp"
  },
  ...
}

And run it with npm run gulp-build. This has a few benefits

  • You can define arguments easily instead of typing them every time
  • Global gulp installation isn't required (same for other tools, like webpack)
  • You can define multiple variants with different environment variables and(or) arguments without changing the gulpfile (as you can see above - gulp and gulp-build for development and production respectively)