Grunt 中的 NodeJS 环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15554215/
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
NodeJS environment variables in Grunt
提问by bevacqua
I'm shifting my project from simply node server.jsinto 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 时设置环境变量(明确)
This isn't an issue when deploying because heroku already takes care of setting my env vars.
部署时这不是问题,因为 heroku 已经负责设置我的 env vars。
回答by hereandnow78
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=productionproduction will be set, otherwise development will be set
在这个例子中,如果你用--environment=production生产调用你的 grunt 任务将被设置,否则开发将被设置

