Background
When you write code there are certain variables that may differ as per the environments like dev, qa prod etc. This might have sensitive data as API keys, passwords etc. You would definitely do not want to put it in your code directly since this will be added to some code repository like git and others may have access to the same.
General practice that is followed is to use environment variables that can be defined at the environment level and can be read and used in the code. For eg. consider Elastic beanstalk or a Lambda in AWS world. You would define environment variables for the environment and use that in code. If it's your our physical box you might define the environment variables at the OS level or maybe at tomcat level if you are using tomcat as the container. Environment variables work fine in all such cases.
But how would you do the same in local. In this post I will show how we can simulate environment variables in a NodeJs process with a package called dotenv.
This post expects you to know basics of NodeJs and have NodeJs and npm (Node package manager) installed on your machine. If you have not done that then please refer my earlier post -
Simulating environment variables in NodeJs using dotenv package
First you need to install dotenv package using npm. To do so go to the directory where you would have your NodeJs file and execute following command -
- npm install dotenv
Now that we have package installed let's see how we can simulate an environment variable. For this simply create a file name .env in the same directory and add the environment variable you expect to read in code in it. For this demo I will use 3 environment variables -
- ENVIRONMENT=local
- USERNAME=athakur
- PASSWORD=athakur
Now create NodeJS file in the same directory. Let's call it test.js. The directory structure is as follows -
Add following content in test.js -
'use strict'; const dotenv = require('dotenv'); dotenv.config(); const env = process.env.ENVIRONMENT const username = process.env.USERNAME const password = process.env.PASSWORD console.log("Env : " + env); console.log("Username : " + username); console.log("Password : " + password);
Save the file and execute it as -
- node test.js
Env : local
Username : athakur
Password : athakur
And that's it. You can add any number of environment variables in the ".env" file and read it in your node js code as process.env.variable_name.
NOTE : .env file would be hidden in Ubuntu since Ubuntu hides all files that start with a dot (.). You can just press Ctrl + H to view hidden files or do a "ls -la" in console. More details -
NOTE : .env file would be hidden in Ubuntu since Ubuntu hides all files that start with a dot (.). You can just press Ctrl + H to view hidden files or do a "ls -la" in console. More details -
To read more about this package you can read -
Related Links
- How to install Node and npm to run node.js programs in Linux(OSFG)
- How to show hidden files and folder in Ubuntu(OSFG)
- https://www.npmjs.com/package/dotenv