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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28787457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:30:05  来源:igfitidea点击:

How can I set an environment variable as gulp task?

node.jsgulpenvironment-variables

提问by chacham15

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

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

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

我宁愿通过 task 从 gulp 中设置环境变量。

What would be a good way to achieve this?

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

回答by Elise Chant

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

回答by rzr

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 run gulp-build. 这有几个好处

  • 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)
  • 您可以轻松定义参数,而不必每次都键入它们
  • 不需要全局 gulp 安装(其他工具也一样,比如 webpack)
  • 您可以使用不同的环境变量和(或)参数定义多个变体,而无需更改 gulpfile(如上所示 - gulp 和 gulp-build 分别用于开发和生产)

回答by talkol

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

回答by gonzalon

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

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

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

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

dev默认情况下,这将始终是env,您可以调用它设置另一个 env,如下所示:

gulp --env prod

gulp --env prod

More of gulp-env

更多的gulp-env

回答by Xabby

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