node.js 节点 process.env.VARIABLE_NAME 返回未定义

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

Node process.env.VARIABLE_NAME returning undefined

node.jsenvironment-variables

提问by Yu Chen

I'm using environment variables on my mac to store some sensitive credentials, and trying to access them through Node. I added them into my environment profile with

我在我的 mac 上使用环境变量来存储一些敏感的凭据,并尝试通过 Node.js 访问它们。我将它们添加到我的环境配置文件中

export VARIABLE_NAME=mySensitiveInfo

export VARIABLE_NAME=mySensitiveInfo

When I use echo $VARIABLE_NAMEI receive the correct output (my sensitive info).

当我使用时,echo $VARIABLE_NAME我会收到正确的输出(我的敏感信息)。

However, when I am trying to access this same variable in Node with process.env.VARIABLE_NAMEand try to print it out on the console, I get an undefined.

但是,当我尝试在 Node 中访问同一个变量process.env.VARIABLE_NAME并尝试在控制台上将其打印出来时,我得到一个未定义的。

Other environment variables appear to be okay though. For example, when I console.log(process.env.FACEBOOK_CALLBACK_URL), it prints the correct value to my console. I added FACEBOOK_CALLBACK_URLa few days ago.

不过其他环境变量似乎没问题。例如,当 I 时console.log(process.env.FACEBOOK_CALLBACK_URL),它会将正确的值打印到我的控制台。几天前我添加了FACEBOOK_CALLBACK_URL

Do I have to restart my machine or something? Does it take a certain time before environment variables become available in Node? The closest answer I've seen on SO is this post, but nobody was able to figure out why it was happening.

我必须重新启动我的机器还是什么?环境变量在 Node 中可用之前是否需要一定时间?我在 SO 上看到的最接近的答案是这篇文章,但没有人能够弄清楚为什么会发生这种情况。

回答by shaochuancs

process.env.VARIABLE_NAMEreturns undefinedbecause the Node.js execution environment does not know the newly added VARIABLE_NAMEyet. To fix the issue, the Node.js execution environment (e.g. IDE) need to restart.

process.env.VARIABLE_NAME返回undefined是因为 Node.js 执行环境还不知道新添加的VARIABLE_NAME。要解决此问题,需要重新启动 Node.js 执行环境(例如 IDE)。

The following steps can be used to reproduce this issue:

可以使用以下步骤来重现此问题:

  1. Open IDE such as WebStorm and write a simple Node.js program: console.log(process.env.VARIABLE_NAME). It will print undefinedas expected, as VARIABLE_NAMEis not defined yet. Keep the IDE running, don't close it.
  2. Open environment profile such as .bash_profileand add export VARIABLE_NAME=mySensitiveInfoin it.
  3. Open system console and run source .bash_profile, so that the above exportstatement will be executed. From now on, whenever system console is opened, VARIABLE_NAMEenvironment variable exists.
  4. In system console, execute the Node.js program in step 1, it will print mySensitiveInfo.
  5. Switch to IDE and execute Node.js program, it will print undefined.
  6. Restart the IDE and execute Node.js program, this time, it will print mySensitiveInfo
  1. 打开WebStorm等IDE,编写一个简单的Node.js程序:console.log(process.env.VARIABLE_NAME). 它将undefined按预期打印,因为VARIABLE_NAME尚未定义。保持 IDE 运行,不要关闭它。
  2. 打开环境配置文件,例如.bash_profile并添加export VARIABLE_NAME=mySensitiveInfo其中。
  3. 打开系统控制台并运行source .bash_profile,这样上面的export语句就会被执行。从现在开始,无论何时打开系统控制台,VARIABLE_NAME环境变量都存在。
  4. 在系统控制台中,执行步骤 1 中的 Node.js 程序,它将打印mySensitiveInfo.
  5. 切换到 IDE 并执行 Node.js 程序,它会打印undefined.
  6. 重启IDE并执行Node.js程序,这次会打印 mySensitiveInfo

回答by kingzez

I got a problem just now , and I use this solved it in webpack config

我刚才遇到了一个问题,我在 webpack 配置中使用它解决了它

const plugins = [
    new webpack.NoEmitOnErrorsPlugin(),
    // after compile global will defined `process.env` this Object
    new webpack.DefinePlugin({
      BUILD_AT : Date.now().toString(32),
      DEBUG: process.env.NODE_ENV !== 'production',
          'process.env': {
              'NODE_ENV': JSON.stringify(process.env.NODE_ENV || "development"),
              'VARIABLE_NAME': JSON.stringify(process.env.VARIABLE_NAME)
     }
   })
]

回答by Sumith Ekanayake

nodemon.jsonfile is only for setting nodemon specific configuration So for create custom environment variables we can use dotenv package

nodemon.json文件仅用于设置 nodemon 特定的配置,因此对于创建自定义环境变量,我们可以使用 dotenv 包

First , Install dotenv package

首先,安装 dotenv 包

npm install dotenv --save

after that create .env file in root and include environment variables as bellows

之后在 root 中创建 .env 文件并包含如下环境变量

MONGO_ATLAS_PW=xxxxx
JWT_KEY=secret

Finally, inside your app.jsfile insert following after your imports.

最后,在app.js导入后在文件中插入以下内容。

require('dotenv').config()

Then you can use environment varibale like this

然后你可以像这样使用环境变量

process.env.MONGO_ATLAS_PW
process.env.JWT_KEY