Javascript 在 Node.js 中读取环境变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4870328/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:32:09  来源:igfitidea点击:

Read environment variables in Node.js

javascriptnode.jsenvironment-variables

提问by Jayesh

Is there a way to read environment variables in Node.js code?

有没有办法在 Node.js 代码中读取环境变量?

Like for example Python's os.environ['HOME'].

例如 Python 的os.environ['HOME'].

回答by Jayesh

process.env.ENV_VARIABLE

Where ENV_VARIABLEis the name of the variable you wish to access.

ENV_VARIABLE您要访问的变量的名称在哪里。

See Node.js docs for process.env.

参见Node.js 文档process.env

回答by Subodh Ghulaxe

When using Node.js, you can retrieve environment variables by key from the process.envobject:

使用Node.js 时,您可以通过键从process.env对象中检索环境变量:

for example

例如

var mode   = process.env.NODE_ENV;
var apiKey = process.env.apiKey; // '42348901293989849243'

Here is the answer that will explain setting environment variables in node.js

这是将解释在 node.js 中设置环境变量的答案

回答by user2574650

If you want to use a string key generated in your Node.js program, say, var v = 'HOME', you can use process.env[v].

如果您想使用在 Node.js 程序中生成的字符串键,例如var v = 'HOME',您可以使用 process.env[v].

Otherwise, process.env.VARNAMEhas to be hardcoded in your program.

否则,process.env.VARNAME必须在您的程序中进行硬编码。

回答by Igor Litvinovich

To retrieve environment variables in Node.JS you can use process.env.VARIABLE_NAME, but don't forget that assigning a property on process.env will implicitly convert the value to a string.

要在 Node.JS 中检索环境变量,您可以使用process.env.VARIABLE_NAME,但不要忘记在 process.env 上分配属性会将值隐式转换为字符串。

Avoid Boolean Logic

避免布尔逻辑

Even if your .env file defines a variable like SHOULD_SEND=falseor SHOULD_SEND=0, the values will be converted to strings(“false” and “0”respectively) and not interpreted as booleans.

即使您的 .env 文件定义了SHOULD_SEND=falseSHOULD_SEND=0 之类的变量,这些值也将转换为字符串(分别为“false”和“0”)而不是解释为布尔值。

if (process.env.SHOULD_SEND) {
 mailer.send();
} else {
  console.log("this won't be reached with values like false and 0");
}

Instead, you should make explicit checks. I've found depending on the environment name goes a long way.

相反,您应该进行明确的检查。我发现根据环境名称有很长的路要走。

 db.connect({
  debug: process.env.NODE_ENV === 'development'
 });

回答by Huy Vo

You can use envpackage to manage your environment variables per project:

您可以使用env包来管理每个项目的环境变量:

  • Create a .envfile under the project directory and put all of your variables there.
  • Add this line in the top of your application entry file:
    require('dotenv').config();
  • .env在项目目录下创建一个文件并将所有变量放在那里。
  • 在应用程序入口文件的顶部添加这一行:
    require('dotenv').config();

Done. Now you can access your environment variables with process.env.ENV_NAME.

完毕。现在您可以使用process.env.ENV_NAME.

回答by Brad Vanderbush

Why not use them in the Users directory in the .bash_profilefile, so you don't have to push any files with your variables to production?

为什么不在.bash_profile文件的 Users 目录中使用它们,这样您就不必将任何带有变量的文件推送到生产中?