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.
We can even load variables from external .env files, by using an npm module called dotenv.
Just by simply including below statement at the start of load file,
It loads all .env variables into process.env object.
Sample .env file:
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=devPORT=3030DBUSER=rootDBPASSWORD=root
Comments
Post a Comment