Node.js:是否有关于 process.env 变量的任何文档
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15058954/
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
Node.js: Is there any documentation about the process.env variable
提问by hh54188
I use process.enva little in my program, it seems this variable have nothing to do with my program, without it my app could work well, too.
我process.env在我的程序中使用了一点,这个变量似乎与我的程序无关,没有它我的应用程序也可以正常运行。
So how can I fully use the process.env? Is there any document or tutorial about it?
那么我怎样才能充分利用process.env? 有没有关于它的文档或教程?
回答by Herman Junge
Try this link http://nodejs.org/api/process.html#process_process_env
试试这个链接http://nodejs.org/api/process.html#process_process_env
Then you can make a small program in nodeJS:
然后就可以在nodeJS中做一个小程序了:
console.log(process.env)
And run it
并运行它
$ node myProgram.js
{ TERM_PROGRAM: 'iTerm.app',
TERM: 'xterm',
SHELL: '/bin/bash',
CLICOLOR: '1',
TMPDIR: '/var/folders/ff/59np25p96x95hpgbtsv3r6zr0000gn/T/',
Apple_PubSub_Socket_Render: '/tmp/launch-LIiu0r/Render',
OLDPWD: '/Users/hermanjunge',
USER: 'hermanjunge',
COMMAND_MODE: 'unix2003',
SSH_AUTH_SOCK: '/tmp/launch-XOMy7j/Listeners',
__CF_USER_TEXT_ENCODING: '0x1F5:0:0',
Apple_Ubiquity_Message: '/tmp/launch-jiZQH0/Apple_Ubiquity_Message',
LSCOLORS: 'ExFxCxDxBxegedabagacad',
PATH: '/Users/hermanjunge/.rbenv/shims:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/usr/local/git/bin:/usr/local/mysql/bin',
PWD: '/tmp',
ITERM_PROFILE: 'hermanjunge',
SHLVL: '1',
COLORFGBG: '7;0',
HOME: '/Users/hermanjunge',
ITERM_SESSION_ID: 'w1t4p0',
LOGNAME: 'hermanjunge',
LC_CTYPE: 'UTF-8',
DISPLAY: '/tmp/launch-HCtQeC/org.macosforge.xquartz:0',
_: '/usr/local/bin/node' }
Then, we learned that we can get elements from the environment we are running our app. Like, for example:
然后,我们了解到我们可以从运行应用程序的环境中获取元素。比如,例如:
console.log(process.env.PWD);
Which returns
哪个返回
/tmp
And so on...
等等...
回答by Aminadav Glickshtein
There is no documentation for the variables of process.envsince it based on your environment. (Surprise).
没有关于变量的文档,process.env因为它基于您的环境。(惊喜)。
When an operation system (OS, Linux, Win, or other), starts a process it's passing it environment variables that the process can read.
当操作系统(OS、Linux、Win 或其他)启动一个进程时,它会向它传递进程可以读取的环境变量。
using process.envyou can read the variables that passed to your programs by the OS.
使用process.env您可以读取操作系统传递给您的程序的变量。
Usually, NodeJS projects are using process.envfor two things:
通常,NodeJS 项目process.env用于两件事:
- Things that need to be changed between environment. For e.g. development, testing, and production. You don't want to connect to real DB during development, and you don't want to show all
console.logon production. - To keep secret. It's unsafe top keep API, tokens, and private keys on Git. So you save set it by using environment variable before starting the app.
- 需要在环境之间改变的东西。例如,开发、测试和生产。您不想在开发过程中连接到真实数据库,也不想
console.log在生产中显示所有内容。 - 为了保密。在 Git 上保留 API、令牌和私钥是不安全的。因此,您可以在启动应用程序之前使用环境变量保存设置。
Pro tip:There is another way. To define things in .envfile. At this file to your .gitignore, and use the npm module dotenv
专业提示:还有另一种方法。在.env文件中定义事物。将此文件添加到您的.gitignore, 并使用 npm 模块dotenv

