Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read environment variables in Node.js

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

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

like image 387
Jayesh Avatar asked Feb 02 '11 02:02

Jayesh


People also ask

How do I read environment variables in node JS?

To retrieve environment variables in Node. JS you can use process. env. VARIABLE_NAME, but don't forget that assigning a property on process.

How do I see npm environment variables?

For a test you can see the env variables by running npm run env-linux or npm run env-windows , and test that they make it into your app by running npm run start-linux or npm run start-windows .

What is .env file in node JS?

The dotenv package for handling environment variables is the most popular option in the Node. js community. You can create an. env file in the application's root directory that contains key/value pairs defining the project's required environment variables.


2 Answers

process.env.ENV_VARIABLE 

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

See Node.js docs for process.env.

like image 182
Jayesh Avatar answered Sep 24 '22 00:09

Jayesh


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

like image 34
Subodh Ghulaxe Avatar answered Sep 24 '22 00:09

Subodh Ghulaxe