节点 process.env.VARIABLE_NAME 返回未定义 [英] Node process.env.VARIABLE_NAME returning undefined

查看:19
本文介绍了节点 process.env.VARIABLE_NAME 返回未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

导出 VARIABLE_NAME=mySensitiveInfo

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

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

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

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

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

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_URL a few days ago.

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

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.

推荐答案

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

process.env.VARIABLE_NAME returns undefined because the Node.js execution environment does not know the newly added VARIABLE_NAME yet. To fix the issue, the Node.js execution environment (e.g. IDE) need to restart.

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

The following steps can be used to reproduce this issue:

  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
  1. Open IDE such as WebStorm and write a simple Node.js program: console.log(process.env.VARIABLE_NAME). It will print undefined as expected, as VARIABLE_NAME is not defined yet. Keep the IDE running, don't close it.
  2. Open environment profile such as .bash_profile and add export VARIABLE_NAME=mySensitiveInfo in it.
  3. Open system console and run source .bash_profile, so that the above export statement will be executed. From now on, whenever system console is opened, VARIABLE_NAME environment 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

这篇关于节点 process.env.VARIABLE_NAME 返回未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆