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.
So how can I fully use the process.env
? Is there any document or tutorial about it?
If you want to take a look at the node's environment variables, you can run the Node. js REPL with “node” in your command line and then type: console. log(process.
In Node. js, process. env is a global variable that is injected during runtime. It is a view of the state of the system environment variables. When we set an environment variable, it is loaded into process.
Environment variables are stored in your system shell that you start node. js from. They are a shell feature that node. js can read/modify.
env files in your project root folder to set such environment variables. For that it appears to be using this package, which you might find useful also in other contexts.
There is no documentation for the variables of process.env
since it based on your environment. (Surprise).
When an operation system (OS, Linux, Win, or other), starts a process it's passing it environment variables that the process can read.
using process.env
you can read the variables that passed to your programs by the OS.
Usually, NodeJS projects are using process.env
for two things:
console.log
on production.Pro tip: There is another way. To define things in .env
file. At this file to your .gitignore
, and use the npm module dotenv
Try this link http://nodejs.org/api/process.html#process_process_env
Then you can make a small program in 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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With