如何将环境变量设置为gulp任务?

时间:2022-01-17 11:12:32

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

我不希望每次运行gulp设置环境变量时都输入额外的参数NODE_ENV='production' gulp。

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

我宁愿通过任务在gulp内部设置环境变量。

What would be a good way to achieve this?

什么是实现这一目标的好方法?

4 个解决方案

#1


115  

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

#2


16  

Try gulp-env

尝试gulp-env

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

在运行nodemon任务之前如何设置一些环境变量的快速示例:

// 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'])

#3


8  

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

您还可以将其定义为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

运行它与npm运行gulp构建。这有一些好处

  • You can define arguments easily instead of typing them every time
  • 您可以轻松地定义参数,而不是每次都输入它们
  • Global gulp installation isn't required (same for other tools, like webpack)
  • 不需要全局gulp安装(对于其他工具,如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)
  • 您可以使用不同的环境变量和(或)参数定义多个变体,而无需更改gulpfile(如上面所示——分别用于开发和生产的gulp和gulp构建)

#4


2  

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

您还可以默认设置一个,并从json文件读取变量:

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:

默认情况下总是dev env,你可以把它叫做设置另一个env,如下所示:

gulp --env prod

杯- env刺激

More of gulp-env

更多的gulp-env

#1


115  

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

#2


16  

Try gulp-env

尝试gulp-env

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

在运行nodemon任务之前如何设置一些环境变量的快速示例:

// 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'])

#3


8  

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

您还可以将其定义为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

运行它与npm运行gulp构建。这有一些好处

  • You can define arguments easily instead of typing them every time
  • 您可以轻松地定义参数,而不是每次都输入它们
  • Global gulp installation isn't required (same for other tools, like webpack)
  • 不需要全局gulp安装(对于其他工具,如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)
  • 您可以使用不同的环境变量和(或)参数定义多个变体,而无需更改gulpfile(如上面所示——分别用于开发和生产的gulp和gulp构建)

#4


2  

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

您还可以默认设置一个,并从json文件读取变量:

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:

默认情况下总是dev env,你可以把它叫做设置另一个env,如下所示:

gulp --env prod

杯- env刺激

More of gulp-env

更多的gulp-env