Read environment variables in Node.js Read environment variables in Node.js javascript javascript

Read environment variables in Node.js


process.env.ENV_VARIABLE

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

See Node.js docs for process.env.


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


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

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