无法在我的Nodejs应用程序中读取我的环境变量

时间:2021-12-24 11:22:32

I am on Ubuntu 12.04 and I'm just learning about environment variables. I am trying to read a custom variable from within my application but it always shows up as undefined. Here is the code of my test app:

我在Ubuntu 12.04上,我只是在学习环境变量。我试图从我的应用程序中读取自定义变量,但它总是显示为未定义。这是我的测试应用程序的代码:

// app.js

console.log('Value: ' + process.env.NODE_ENV);

If I run the following commands you will see that the variable has a value:

如果我运行以下命令,您将看到该变量具有值:

$ NODE_ENV=production
$ echo $NODE_ENV
production

I can echo $NODE_ENV all day and it will continue to show me "production", but when I do process.env.NODE_ENV in my Node application it always displays "undefined".

我可以整天回复$ NODE_ENV并继续向我显示“生产”,但是当我在我的Node应用程序中处理process.env.NODE_ENV时,它总是显示“undefined”。

$ node app.js
Value: undefined

Here is the odd part though, if I display another environment variable that I know already exists, say process.env.PATH, then it works.

这是奇怪的部分,如果我显示另一个我知道已经存在的环境变量,比如process.env.PATH,那么它可以工作。

$ node app.js
Value: /usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

Another quirk is that the command printenv list doesn't appear to contain my custom variable NODE_ENV despite the fact that echo $NODE_ENV shows me the correct value. printenv NODE_ENV shows nothing as well, but printenv PATH shows the proper value just as it did when I accessed PATH in my node application.

另一个怪癖是命令printenv列表似乎不包含我的自定义变量NODE_ENV,尽管echo $ NODE_ENV显示了正确的值。 printenv NODE_ENV也没有显示任何内容,但是printenv PATH显示正确的值,就像我在节点应用程序中访问PATH时一样。

3 个解决方案

#1


21  

You need to export shell variables in order to make them available to processes you execute in your shell.

您需要导出shell变量,以使它们可用于您在shell中执行的进程。

Compare the output of this command:

比较此命令的输出:

FOO=bar; bash -c 'echo $FOO'

with the output of this:

与此输出:

export FOO=bar; bash -c 'echo $FOO'

#2


6  

I found my way here from something really silly.

我从一些非常愚蠢的东西找到了我的路。

I had just added the new exported variables, but my node process still wasn't seeing them. Then I realized it wasn't enough to restart the node process—I had to open a new terminal (ie. bash instance) too. Once I did this, it worked fine :)

我刚刚添加了新的导出变量,但我的节点进程仍然没有看到它们。然后我意识到重启节点进程是不够的 - 我不得不打开一个新的终端(即bash实例)。一旦我这样做,它工作得很好:)

#3


0  

You might want to consider using a library for managing app configuration.

您可能需要考虑使用库来管理应用程序配置。

For example nconf helps manage configuration through

例如,nconf有助于管理配置

  • command line argumets
  • 命令行争论
  • environment variables
  • 环境变量
  • files
  • etc..
  • 等等..

And looking at the source is a nice way to learn https://github.com/flatiron/nconf

查看源代码是学习https://github.com/flatiron/nconf的好方法

#1


21  

You need to export shell variables in order to make them available to processes you execute in your shell.

您需要导出shell变量,以使它们可用于您在shell中执行的进程。

Compare the output of this command:

比较此命令的输出:

FOO=bar; bash -c 'echo $FOO'

with the output of this:

与此输出:

export FOO=bar; bash -c 'echo $FOO'

#2


6  

I found my way here from something really silly.

我从一些非常愚蠢的东西找到了我的路。

I had just added the new exported variables, but my node process still wasn't seeing them. Then I realized it wasn't enough to restart the node process—I had to open a new terminal (ie. bash instance) too. Once I did this, it worked fine :)

我刚刚添加了新的导出变量,但我的节点进程仍然没有看到它们。然后我意识到重启节点进程是不够的 - 我不得不打开一个新的终端(即bash实例)。一旦我这样做,它工作得很好:)

#3


0  

You might want to consider using a library for managing app configuration.

您可能需要考虑使用库来管理应用程序配置。

For example nconf helps manage configuration through

例如,nconf有助于管理配置

  • command line argumets
  • 命令行争论
  • environment variables
  • 环境变量
  • files
  • etc..
  • 等等..

And looking at the source is a nice way to learn https://github.com/flatiron/nconf

查看源代码是学习https://github.com/flatiron/nconf的好方法