简体   繁体   English

Grunt中的NodeJS环境变量

[英]NodeJS environment variables in Grunt

I'm shifting my project from simply node server.js into using Grunt. 我正在将我的项目从简单的node server.js转移到使用Grunt。

I used to run my application directly from webstorm, and environment variables would be set up for me. 我曾经直接从webstorm运行我的应用程序,并且会为我设置环境变量。

How can I achieve the same in Grunt? 我怎样才能在Grunt中实现同样的目标?

I need to either run grunt from webstorm (windows), or set up env vars when running grunt (explicitly) 我需要从webstorm(windows)运行grunt,或者在运行grunt(显式)时设置env变量

This isn't an issue when deploying because heroku already takes care of setting my env vars. 这在部署时不是问题,因为heroku已经负责设置我的环境变量。

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

and set your config: 并设置您的配置:

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

in your gruntfile you will probably define some default-task: 在你的gruntfile中你可能会定义一些默认任务:

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

so if you run 'grunt default' at first your env-vars are set, and then 'another-task' is run 因此,如果您首先运行'grunt default',则设置env-vars,然后运行'another-task'

if you want to specify the current environment via command-line option you could use grunt.option: 如果要通过命令行选项指定当前环境,可以使用grunt.option:

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

in this example if you call your grunt task with --environment=production production will be set, otherwise development will be set 在此示例中,如果使用--environment=production生产--environment=production生产--environment=production生产,则将设置--environment=production生产

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM