Javascript 在 package.json 中设置 process.env var
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42624054/
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
Setting process.env var in package.json
提问by MChan
I am trying to set and retrieve node app process.env vars using package.json, so by researching the issue, I've found an example to set / retrieve process.env through the 'config' section, so I added a new config section as shown below :
我正在尝试使用 package.json 设置和检索节点应用程序 process.env 变量,因此通过研究该问题,我找到了一个通过“配置”部分设置/检索 process.env 的示例,因此我添加了一个新配置部分如下图:
"config" : { "var1" : "test", "var2" : "test2", "var3" : "test3" },
But I couldn't access any of the above vars from server.js using for example:
但是我无法从 server.js 访问上述任何变量,例如:
console.log(process.env.npm_package_config_var1);
So I was wondering how I can set / retrieve process.env var using package.json? Thanks
所以我想知道如何使用 package.json 设置/检索 process.env var?谢谢
*I am using npm 4.4.1, node 7.4.0 and I run the app using (npm run dev)
*我正在使用 npm 4.4.1,节点 7.4.0,我使用 (npm run dev) 运行应用程序
回答by rsp
You cannot just set environment variables in package.json.
您不能只在 package.json 中设置环境变量。
You can set them in your script sections using:
您可以使用以下方法在脚本部分中设置它们:
"scripts": {
"start": "ENV_VAR=abc node app.js",
},
or:
或者:
"scripts": {
"start": "cross-env ENV_VAR=abc node app.js",
},
using the cross-env module. See:
使用跨环境模块。看:
Environment variables are something that your programs get at runtime, not something stored in a config - unless you use something like dotenv, see:
环境变量是您的程序在运行时获得的东西,而不是存储在配置中的东西 - 除非您使用类似 dotenv 的东西,请参阅:
but this is using the .envfile, not package.json.
但这是使用.env文件,而不是 package.json。
回答by Flash
You cannot just set environment variables in package.json.
您不能只在 package.json 中设置环境变量。
You may try out node -p process.envas your npm script to inspect your env variable. And ensure that nothing else overwrites your values. Here is another examplewhich works for me.
您可以尝试node -p process.env作为您的 npm 脚本来检查您的 env 变量。并确保没有其他东西会覆盖您的价值观。这是另一个对我有用的例子。
回答by Steeve Pitis
I don't really understand, what are you trying to do.
我真的不明白,你想做什么。
But if you want to retrieve env variables you have to do define your dev script in your package.jsonlike this : NODE_ENV=dev node index.js
但是如果你想检索环境变量,你必须package.json像这样定义你的开发脚本:NODE_ENV=dev node index.js
Then fetch your env with : process.env.NODE_ENV
然后使用以下命令获取您的环境: process.env.NODE_ENV
回答by Paul Bartlett
This will only work if you start your application using npm:
这仅在您使用 npm 启动应用程序时才有效:
so have:
所以有:
"scripts": {
"start": "node server.js"
},
"config" : { "var1" : "test", "var2" : "test2", "var3" : "test3" }
then:
然后:
npm run start
and within server.js this will display the variable correctly:
在 server.js 中,这将正确显示变量:
console.log(process.env.npm_package_config_var1);

