Hassle Free Development: Manage Environment in Node App
A lifelong learner. Love to travel. Listen to music.
Search for a command to run...
A lifelong learner. Love to travel. Listen to music.
No comments yet. Be the first to comment.
Welcome to Javascript world. The popularity of javascript has brought dramatically changed in app development paradigm. And server-side development revolution started with the Node.js. According to the node.js official site, Node.js is Asynchronous ...
Best Practices for Path Structure, Versioning, and Error Handling

Understanding the Performance Implications of async and defer Attributes

Overview static: Default position. It does not allow setting properties like top, bottom, left, right, z-index relative: Acts the same as static. but allows positioning it relatively by putting properties like top, bottom, left, right, z-index absolu...

Overview When an element in the browser has a conflict of styles, the browser uses a set of rules to determine which style should be rendered. This set of rules is defined as CSS Selector Specificity. For instance, for an element, if we have the foll...

Master block, inline, flex, and grid to build better web layouts

Get the final source code from here
Environment variables are injected from outside the application. These variables are varied in a different stage of the application. For example, a web app has separate database addresses for
Development Mode
Testing Mode
Staging Mode
Execution Mode
So when you are developing an app, definitely you do not want to mess up with production database. In this criteria, managing environment variable comes handy.
To manage the environment variable, here we are going to use the following tools
Visual Studio Code (code editor)
dotenv (npm package)
DotENV (editor extension)
Install the editor for your suitable platform
Install Node.js in your machine
Make sure both node and npm is installed in your system
Open Visual Studio Code and install the dotEnv extension.
To install dotENV extension in Visual Studio Code
Open the code editor
Open vs code extension panel (Crtl+Shift+X)
Search for dotENV
Install the extension
Create a directory, manageEnvironment and enter the directory.
mkdir manageEnvironment
cd manageEnvironment
Now in the root directory, create the project
npm init -y
Now open the directory in your code editor.
Create two files named index.js and .env
Your project directory should look like
├── ...
├── manageEnvironment
│ ├── index.js
│ ├── .env
│ └── package.json
├── ...
Install the npm package dotenv from the root directory.
npm i dotenv
Update your .env file with an environment variable and your .env file should look like
foo=bar
And now invoke the dotEnv and read the environment variable. Your index.js file should be look like
const dotenv = require(“dotenv”);
dotenv.config();
console.log(process.env.foo);
Now run your node app by
node index.js
In your console, output should be looked like,
bar
Always put the .env file in .gitIgnore
So create the .gitIgnore file and add the .env file.
Your .gitIgnore file should be looked like,
.env
So when your code is in development, testing, staging or production phase, use the appropriate environment variable.
Stay tuned and if there is a confusing term or something, the response below. I will replay ASAP.