node.js 如何从 package.json 中设置环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25112510/
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
How to set environment variables from within package.json
提问by dev.meghraj
How to set some environment variables from within package.jsonto be used with npm startlike commands?
如何从内部设置一些环境变量package.json以与npm start类似命令一起使用?
Here's what I currently have in my package.json:
这是我目前在我的package.json:
{
...
"scripts": {
"help": "tagove help",
"start": "tagove start"
}
...
}
I want to set environment variables (like NODE_ENV) in the start script while still being able to start the app with just one command, npm start.
我想NODE_ENV在启动脚本中设置环境变量(如),同时仍然能够仅使用一个命令启动应用程序npm start.
回答by cesar
Set the environment variable in the script command:
在脚本命令中设置环境变量:
...
"scripts": {
"start": "node app.js",
"test": "env NODE_ENV=test mocha --reporter spec"
},
...
Then use process.env.NODE_ENVin your app.
然后process.env.NODE_ENV在您的应用程序中使用。
Note: envensures that it works across platforms. You can omit it if you only care about Mac/Linux.
注意:env确保它跨平台工作。如果您只关心 Mac/Linux,则可以省略它。
回答by TetraDev
Just use NPM package cross-env. Super easy. Works on Windows, Linux, and all environments. Notice that you don't use && to move to the next task. You just set the env and then start the next task. Credit to @mikekidderfor the suggestion in one of the commentshere.
只需使用 NPM 包cross-env。超级容易。适用于 Windows、Linux 和所有环境。请注意,您不使用 && 移动到下一个任务。您只需设置环境,然后开始下一个任务。感谢@mikekidder在建议的意见,一个在这里。
From documentation:
从文档:
{
"scripts": {
"build": "cross-env NODE_ENV=production OTHERFLAG=myValue webpack --config build/webpack.config.js"
}
}
Notice that if you want to set multiple global vars, you just state them in succession, followed by your command to be executed.
请注意,如果要设置多个全局变量,只需依次声明它们,然后执行要执行的命令。
Ultimately, the command that is executed (using spawn) is:
最终,执行的命令(使用 spawn)是:
webpack --config build/webpack.config.js
The NODE_ENVenvironment variable will be set by cross-env
该NODE_ENV环境变量将通过交ENV被设置
回答by TeemuK
I just wanted to add my two cents here for future Node-explorers. On my Ubuntu 14.04 the NODE_ENV=testdidn't work, I had to use export NODE_ENV=testafter which NODE_ENV=teststarted working too, weird.
我只是想在这里为未来的 Node-explorers 添加我的两分钱。在我的 Ubuntu 14.04 上NODE_ENV=test不起作用,我不得不使用它export NODE_ENV=test之后也NODE_ENV=test开始工作,很奇怪。
On Windows as have been said you have to use set NODE_ENV=testbut for a cross-platform solution the cross-env library didn't seem to do the trick and do you really need a library to do this:
在 Windows 上,您必须使用,set NODE_ENV=test但对于跨平台解决方案,跨环境库似乎没有用,您是否真的需要一个库来执行此操作:
export NODE_ENV=test || set NODE_ENV=test&& yadda yadda
The vertical bars are needed as otherwise Windows would crash on the unrecognized export NODE_ENVcommand :D. Dunno about the trailing space but just to be sure I removed them too.
需要竖线,否则 Windows 会在无法识别的export NODE_ENV命令上崩溃:D。不知道尾随空间,但只是为了确保我也删除了它们。
回答by Luke
Because I often find myself working with multiple environment variables, I find it useful to keep them in a separate .envfile (make sure to ignore this from your source control).
因为我经常发现自己使用多个环境变量,所以我发现将它们保存在单独的.env文件中很有用(确保从源代码管理中忽略这一点)。
VAR_A=Hello World
VAR_B=format the .env file like this with new vars separated by a line break
Then prepend export $(cat .env | xargs) &&before your script command.
然后export $(cat .env | xargs) &&在您的脚本命令之前添加。
Example:
例子:
{
...
"scripts": {
...
"start": "export $(cat .env | xargs) && echo do your thing here",
"env": "export $(cat .env | xargs) && env",
"env-windows": "export $(cat .env | xargs) && set"
}
...
}
For a test you can view the env variables by running npm run env(linux) or npm run env-windows(windows).
对于测试,您可以通过运行npm run env(linux) 或npm run env-windows(windows)查看 env 变量。
回答by Pascal Mayr
Try this on Windows by replacing YOURENV:
通过替换在 Windows 上试试这个YOURENV:
{
...
"scripts": {
"help": "set NODE_ENV=YOURENV && tagove help",
"start": "set NODE_ENV=YOURENV && tagove start"
}
...
}
回答by dev.meghraj
suddenly i found that actionhero is using following code, that solved my problem by just passing --NODE_ENV=productionin start script command option.
突然间我发现 actionhero 正在使用以下代码,通过传入--NODE_ENV=productionstart script 命令选项解决了我的问题。
if(argv['NODE_ENV'] != null){
api.env = argv['NODE_ENV'];
} else if(process.env.NODE_ENV != null){
api.env = process.env.NODE_ENV;
}
i would really appreciate to accept answer of someone else who know more better way to set environment variables in package.json or init script or something like, where app bootstrapped by someone else.
我真的很感激接受其他人的回答,他们知道在 package.json 或 init 脚本或类似的东西中设置环境变量的更好方法,其中应用程序由其他人引导。
回答by KARASZI István
回答by Sergey
This will work in Windows console:
这将在Windows 控制台中工作:
"scripts": {
"aaa": "set TMP=test && npm run bbb",
"bbb": "echo %TMP%"
}
npm run aaa
npm run aaa
output:
test
输出:
test
See this answerfor details.
有关详细信息,请参阅此答案。
回答by Luiz Henrique Martins Lins Rol
Although not directly answering the question I′d like to share an idea on top of the other answers. From what I got each of these would offer some level of complexity to achieve cross platform independency.
虽然没有直接回答这个问题,但我想在其他答案之上分享一个想法。从我得到的每一个都会提供一定程度的复杂性来实现跨平台独立性。
On my scenario all I wanted, originally, to set a variable to control whether or not to secure the server with JWT authentication (for development purposes)
在我的场景中,最初我想要设置一个变量来控制是否使用 JWT 身份验证保护服务器(用于开发目的)
After reading the answers I decided simply to create 2 different files, with authentication turned on and off respectively.
阅读答案后,我决定简单地创建 2 个不同的文件,分别打开和关闭身份验证。
"scripts": {
"dev": "nodemon --debug index_auth.js",
"devna": "nodemon --debug index_no_auth.js",
}
The files are simply wrappers that call the original index.js file (which I renamed to appbootstrapper.js):
这些文件只是调用原始 index.js 文件(我重命名为appbootstrapper.js)的包装器:
//index_no_auth.js authentication turned off
const bootstrapper = require('./appbootstrapper');
bootstrapper(false);
//index_auth.js authentication turned on
const bootstrapper = require('./appbootstrapper');
bootstrapper(true);
class AppBootStrapper {
init(useauth) {
//real initialization
}
}
Perhaps this can help someone else
也许这可以帮助别人
回答by Cailean
{
...
"scripts": {
"start": "ENV NODE_ENV=production someapp --options"
}
...
}

