Search code examples
node.jsgulpenvironment-variables

How can I set an environment variable as gulp task?


I don't want type the extra arguments NODE_ENV='production' gulp every time I run gulp to set an environment variable.

I would rather set the environment variable from within gulp via a task.

What would be a good way to achieve this?


Solution

  • 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'
        );
    });