Most Popular Tutorials
Most Popular Tutorials :-

Simply Easy Learning at Your Fingertips. Click Tutorials Menu to view More Tutorial List





.env and How to use .env file in Node.Js application

The .env file for handling environment variables. it is the most popular option in the Node.js.

we can create .env file in the application's root directory. That contains key/value pairs defining the project's required environment variables.

The .env library reads this.env file and appends it to process.env.

install dotenv package

File Name :

npm i dotenv --save

File Name :

require('dotenv').config()

File Name :

require('dotenv').config()
const db = require('db')
db.connect({
host: process.env.DB_HOST,
username: process.env.DB_USER,
password: process.env.DB_PASS
})

1. Add .env to gitignore

File Name :

# Environment variables.
STATUS=production
#Development port
DEV_PORT=3000
#Production port
PROD_PORT=8000

#DB CONFIG
HOST=db.host
USER=root
PASSWORD=db.password
DB=db.name
DIALECT=mysql

File Name :

1. Commit the changes to your repository

git add .gitignore
git comit -m "adding .env.to .gitinore"

File Name :

NOTE
.env includes sensitive information, so it should not be pushed with your code/GIT repo. Please make sure!
This is how you can avoid .env to push in your repo using GIT.

Create a file called .gitignore in the root directory of your project, then open it, then write .env in it. Now git will avoid pushing your .env file

File Name :

We can attempt to parse an integer value from the PORT environment variable, or default to Port 5000 if the environment variable is not defined or contains something other than an integer:

const port = parseInt(process.env.PORT, 10) || 5000;
The name returned in the HTTP response can be found in the MYNAME environment variable or defaults to the string mahi:

const name = process.env.MYNAME || "mahi"

File Name :

http.createServer((request, response) => {
// ...
}).listen(port);

How to use .env file in node js application?

File Name :

  • Step 1. First create .env file in your root direcory of node application.
  • Step 2. set db configuration and others :-
    MYSQL_HOST=localhost
    MYSQL_USER=root
    MYSQL_PASSWORD=root
    MYSQL_PORT=3306
    MYSQL_DATABASE=database_name
    #!JWT_KEY=mahtab
    SECRET=mahtab
    PORT=3000
    #!MOD_ENV=prod
    NODE_ENV=development
  • Step 3. npm install dotenv
    ############ OR #####
    npm install dotenv --save-dev
  • Step 4. set app.js file.
    const dotenv = require('dotenv');
    dotenv.config();

  • Database connection using .env file

    File Name :

    const { createPool } = require('mysql');

    const connection = createPool({
    host:process.env.MYSQL_HOST,
    // port:process.env.MYSQL_PORT,
    user:process.env.MYSQL_USER,
    password:process.env.MYSQL_PASSWORD,
    database:process.env.MYSQL_DATABASE,
    connectionLimit:10

    }); module.exports = connection;

    config file

    File Name : config.js

    const dotenv = require('dotenv');
    dotenv.config();
    module.exports = {
    endpoint: process.env.API_URL,
    masterKey: process.env.API_KEY,
    port: process.env.PORT
    };

    config file

    The dotenv.config() function from the dotenv npm package will read the .env file, assign the variables to process.env, and return an object containing the content. it will also throw an error if it failed.

    File Name : config.js

    const dotenv = require('dotenv');
    const result = dotenv.config();
    if (result.error) {
    throw result.error;
    }
    const { parsed: envs } = result;
    console.log(envs);
    module.exports = envs;

    You can then export this object from a module and make it easily accessible in your app once again.

    const { endpoint, masterKey, port } = require(‘./config’);





    Previous Next


    Trending Tutorials




    Review & Rating

    0.0 / 5

    0 Review

    5
    (0)

    4
    (0)

    3
    (0)

    2
    (0)

    1
    (0)

    Write Review Here