61

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?

hh54188
  • 14,887
  • 32
  • 113
  • 184

2 Answers2

80

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

Herman Junge
  • 2,759
  • 24
  • 20
  • Can I add or change some field? – hh54188 Feb 25 '13 at 02:19
  • 19
    Run your program with the variable of interest. Example: `$ MYVAR=somevalue node myProgram.js`, then access it by its name: `console.log(process.env.MYVAR);` – Herman Junge Feb 25 '13 at 02:20
  • 1
    If I want add more than one variable what should I do?Is there more tips like this?Where can I learn them? – hh54188 Feb 25 '13 at 02:30
  • 11
    This way: `$ MYVAR=somevalue OTHERVAR=othervalue node myProgram.js` – Herman Junge Feb 25 '13 at 02:35
  • 1
    You can learn a lot reading the nodeJS docs, and I advice you to play a lot with tests programs using `console.log()`. – Herman Junge Feb 25 '13 at 02:36
  • 3
    Or you can use [node-inspector](https://github.com/node-inspector/node-inspector) to learn about how your node app works. – knownasilya Oct 29 '13 at 17:41
  • 2
    This could have been added to node in later versions, but you can now do: node -p process.env No need to save a file first – Mark Hagers May 25 '16 at 16:00
  • 1
    Running a test program is not documentation, unless it can be run on all possible versions on all possible operating systems. – giorgiosironi Jun 14 '16 at 15:18
  • I think you missed the first line of the answer @giorgiosironi, where a link to the documentation is pointed. I'm sorry that a complementary example has bothered you that much!!! :-) – Herman Junge Jun 14 '16 at 16:40
  • Sorry, the frustration of ever-changing env variables :) This is not a Node.js responsibility: most of them come from the underlying OS I guess. – giorgiosironi Jun 15 '16 at 09:54
3

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:

  1. 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.log on production.
  2. 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.

Pro tip: There is another way. To define things in .env file. At this file to your .gitignore, and use the npm module dotenv

Aminadav Glickshtein
  • 23,232
  • 12
  • 77
  • 117
  • Loren's West config might be a better alternative in 2022 year. https://www.npmjs.com/package/config (there are few people referred pm2). From my perspective different config files should be defined for each environment and by just supplying right env name we can run app with different env variables. Although it seems dotenv allows only single .env file, going further adding different scripts for start app under different envs in package,json is not easy as it should be. Just a feedback from the quick overview. – Joe Dow May 11 '22 at 12:55