bash 无法在我的 Node.js 应用程序中读取我的环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10803653/
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
Can't read my environment variable in my Node.js app
提问by Chev
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 上,我只是在学习环境变量。我正在尝试从我的应用程序中读取自定义变量,但它始终显示为undefined. 这是我的测试应用程序的代码:
// 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_ENVall day and it will continue to show me "production", but when I do process.env.NODE_ENVin my Node application it always displays "undefined".
我可以echo $NODE_ENV一整天,它会继续向我显示“生产”,但是当我process.env.NODE_ENV在我的 Node 应用程序中这样做时,它总是显示“未定义”。
$ 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 listdoesn't appear to contain my custom variable NODE_ENVdespite the fact that echo $NODE_ENVshows me the correct value. printenv NODE_ENVshows nothing as well, but printenv PATHshows the proper value just as it did when I accessed PATHin my node application.
另一个怪癖是该命令printenv list似乎不包含我的自定义变量,NODE_ENV尽管它echo $NODE_ENV向我显示了正确的值。printenv NODE_ENV也没有显示任何内容,但printenv PATH显示了正确的值,就像我PATH在节点应用程序中访问时所做的那样。
回答by lanzz
You need to exportshell variables in order to make them available to processes you execute in your shell.
您需要exportshell 变量,以使它们可用于您在 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'
回答by MalcolmOcean
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 实例)。一旦我这样做了,它就可以正常工作:)
回答by 250R
You might want to consider using a library for managing app configuration.
您可能需要考虑使用库来管理应用程序配置。
For example nconfhelps 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的好方法
回答by J.Villalon
Restart your bash (source ~/.bashrc). This will take into account your system environment.
重新启动你的 bash (source ~/.bashrc)。这将考虑到您的系统环境。

