Read environment variables in Node.js


Question

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

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

1
1206
10/18/2018 1:16:21 AM

Accepted Answer

process.env.ENV_VARIABLE

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

See Node.js docs for process.env.

1787
12/10/2016 7:24:19 PM

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


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon