70

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?

lorem monkey
  • 3,942
  • 3
  • 35
  • 49
chacham15
  • 13,719
  • 26
  • 104
  • 207
  • 2
    use `yargs` & `gulp-constatnt` to generate the files having `env` based on command line arguments passed – harishr Mar 01 '15 at 13:52

5 Answers5

143
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'
    );
});
Elise Chant
  • 5,048
  • 3
  • 29
  • 36
22

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)
MayThrow
  • 2,159
  • 4
  • 24
  • 38
  • Great answer. I think it is so important to try to leverage what you already have, before introduction another tool into the mix! – Red2678 Jun 07 '17 at 19:39
17

Try gulp-env

Quick example on how to set some environment variables before running the nodemon task:

// gulpfile.js

var gulp = require('gulp');
var nodemon = require('nodemon');
var env = require('gulp-env');

gulp.task('nodemon', function() {
    // nodemon server (just an example task)
});

gulp.task('set-env', function () {
  env({
    vars: {
      MONGO_URI: "mongodb://localhost:27017/testdb-for-british-eyes-only",
      PORT: 9001
    }
  })
});

gulp.task('default', ['set-env', 'nodemon'])
talkol
  • 12,564
  • 11
  • 54
  • 64
  • 1
    When I run this, I get `MONGO_URI is not defined` – Dallin Aug 23 '16 at 16:35
  • The native Node way is probably the best as shown in other answers on this post. ` return process.env.NODE_ENV = 'production';` – dman Dec 05 '17 at 19:25
3

You can Setup the environment like follows:

// Environment Setup
var env = process.env.NODE_ENV || 'development';

Then you can use the environment to process the code as follows:

gulp.task('js',function(){
gulp.src(jsPath)
.pipe(browserify({debug: env === 'development'}))
.pipe(gulpif(env === 'production' , uglify()))
.pipe(gulp.dest(jsDest));
});
Xabby
  • 435
  • 6
  • 26
2

You can also set one by default, and read the variables from a json file:

gulp.task('set-env', function () {
    var envId = gutil.env.env;
    if (!envId) {
      envId = "dev";
    }
    genv({
        file: "env." + envId + ".json"
    });
});

This would be always dev env by default, and you could call it setting another env, like this:

gulp --env prod

More of gulp-env

Gonz
  • 1,198
  • 12
  • 26