How to use dotenv in js project

 

//first install this
npm install dotenv

//then add this on the top of the your code
require(“dotenv”).config();

//first create file with name of .env
DB_HOST=localhost
DB_USER=root
DB_PASS=s1mpl3

//then use it
const db = require(‘db’)
db.connect({
host: process.env.DB_HOST,
username: process.env.DB_USER,
password: process.env.DB_PASS
})

Leave a Comment

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