简体   繁体   English

Node.js:有关于process.env变量的任何文档

[英]Node.js: Is there any documentation about the process.env variable

I use process.env a 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 ? 那么我怎样才能完全使用process.env Is there any document or tutorial about it? 有关于它的任何文件或教程吗?

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... 等等...

There is no documentation for the variables of process.env since it based on your environment. process.env的变量没有文档,因为它基于您的环境。 (Surprise). (惊喜)。

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.env you can read the variables that passed to your programs by the OS. 使用process.env您可以读取操作系统传递给您的程序的变量。

Usually, NodeJS projects are using process.env for two things: 通常,NodeJS项目使用process.env做两件事:

  1. Things that need to be changed between environment. 环境之间需要改变的事情。 For eg development, testing, and production. 例如开发,测试和生产。 You don't want to connect to real DB during development, and you don't want to show all console.log on production. 您不希望在开发期间连接到真正的数据库,并且您不希望在生产中显示所有console.log
  2. To keep secret. 保守秘密 It's unsafe top keep API, tokens, and private keys on Git. 在Git上保存API,令牌和私钥是不安全的。 So you save set it by using environment variable before starting the app. 因此,您可以在启动应用程序之前使用环境变量进行保存设置。

Pro tip: There is another way. 专业提示:还有另一种方式。 To define things in .env file. .env文件中定义内容。 At this file to your .gitignore , and use the npm module dotenv 在此文件中输入.gitignore ,并使用npm模块dotenv

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM