Simple way to use environment variables in nodejs


This page helps you to know about environment variable usage in nodejs.

Nodejs contains a global object process which contains a property env object. Nodejs updates all environment variables into env on boot-up. We can check all environment variables by logging.

console.log(process.env);

We can even load variables from external .env files, by using an npm module called dotenv.

npm install dotenv --save

Just by simply including below statement at the start of load file,

require('dotenv').load();

It loads all .env variables into process.env object.

Sample .env file:

ENV=dev
PORT=3030
DBUSER=root
DBPASSWORD=root 

Comments

Popular posts from this blog

conditional subschemas (writing conditions inside a JSON Schema)

JSON Schema validation keywords with more explanation

nodejs/javascript client for Rabbitmq