Read environment variables in Node.js

03/28/2020 02:00:02

Is there a way to read environment variables in Node.js code?

Like for example Python's os.environ['HOME'].

Verified Answer (1863 Votes)

02/02/2011 08:14:13
process.env.ENV_VARIABLE

Where ENV_VARIABLE is the name of the variable you wish to access.

See Node.js docs for process.env.

1863

Answer #2 (133 Votes)

10/31/2015 10:42:11

When using Node.js, you can retrieve environment variables by key from the process.env object:

for example

var mode   = process.env.NODE_ENV;
var apiKey = process.env.apiKey; // '42348901293989849243'

Here is the answer that will explain setting environment variables in node.js

133

Answer #3 (57 Votes)

07/12/2013 04:39:53

If you want to use a string key generated in your Node.js program, say, var v = 'HOME', you can use process.env[v].

Otherwise, process.env.VARNAME has to be hardcoded in your program.

57
3
Hack Hex uses Stack Exchance API by the Stack Exchange Inc. to scrape questions/answers under Creative Commons license.