Grunt中的NodeJS环境变量 [英] NodeJS environment variables in Grunt

查看:147
本文介绍了Grunt中的NodeJS环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将项目从简单的 node server.js 转移到使用Grunt。

I'm shifting my project from simply node server.js into using Grunt.

直接从网络风暴中运行我的应用程序,并为我设置环境变量。

I used to run my application directly from webstorm, and environment variables would be set up for me.

我如何在Grunt中实现相同?

How can I achieve the same in Grunt?

我需要从网络风暴(Windows)运行grunt,或者在运行grunt(显式)时设置env var)

I need to either run grunt from webstorm (windows), or set up env vars when running grunt (explicitly)

这不是问题在部署时,因为heroku已经照顾了设置我的env vars。

This isn't an issue when deploying because heroku already takes care of setting my env vars.

推荐答案

使用grunt-env插件: https://npmjs.org/package/grunt-env

use the grunt-env plugin: https://npmjs.org/package/grunt-env

并设置您的配置:

grunt.initConfig({
  env : {
    options : {
      //Shared Options Hash
    },
    dev : {
      NODE_ENV : 'development',
      DEST     : 'temp'
    }
  },
  'another-task': {}
});

grunt.registerTask('default', ['env', 'another-task']);

所以如果你运行'grunt默认',首先你的env-var被设置,然后'另一个-task'运行

so if you run 'grunt default' at first your env-vars are set, and then 'another-task' is run

如果要通过命令行选项指定当前环境,可以使用grunt.option:

if you want to specify the current environment via command-line option you could use grunt.option:

grunt.initConfig({
  env : {
    options : {
      //Shared Options Hash
    },
    dev : {
      NODE_ENV : grunt.option('environment') || 'development',
      DEST     : 'temp'
    }
  },

在此示例中,如果您使用调用您的grunt任务 - 环境=生产生产将被设置,否则开发将被设置

in this example if you call your grunt task with --environment=production production will be set, otherwise development will be set

这篇关于Grunt中的NodeJS环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆