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

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

问题描述

我正在将我的项目从简单的 node server.js 转变为使用 Grunt.

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

我以前直接从 webstorm 运行我的应用程序,并且会为我设置环境变量.

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?

我需要从 webstorm(windows)运行 grunt,或者在运行 grunt(显式)时设置环境变量

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

部署时这不是问题,因为 heroku 已经负责设置我的环境变量.

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

在您的 gruntfile 中,您可能会定义一些默认任务:

in your gruntfile you will probably define some default-task:

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

所以如果你首先运行grunt default",你的环境变量被设置,然后另一个任务"被运行

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'
    }
  },

在本例中,如果您使用 --environment=production 调用 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天全站免登陆