×
Environment variables and NodeJS Application – Linux


Environment variables and NodeJS Application – Linux

February 11, 2019

The system environment is primarily the set of variables that define or control certain aspects of process execution. Environment variables help you differentiate between various environments in which your app is running and multiple apps in the environment can access these. Environment variables are system specific and are editable.

 

Environment variables help you to run your Node.js code in different environments like local/production/development systems. Various environments that we come across are

local – Your local machine  environment
sit – system integration testing environment
uat – user acceptance testing environment
development – Your development machine environment
production environment
staging environment

 

LINUX

Method 1
To set environment variable use code

See your environment variables in Linux

This command displays environment variables

Example output

 

Now access environment variables inside Node.js code

Gives output as local

the above code gives local as your environment

Method 2

Accessing environment variables in Node.js is supported as a default. When your Node.js process starts it will automatically provide access to all existing environment variables.

(app.js is your entry file)

Gives output as local

Method 3 using dotenv module

 

Require this module using

Now create .env file in the application root folder and add,

Inside this

Gives output as local

No Comments

Leave a Reply

Your email address will not be published. Required fields are marked *