Environment Variables
dotenv
Guide Medium Article
Install
npm i dotenv
Create a file
.env
in root directory
.env
MONGO_URI=mongodb://localhost:27017/express-demo
- Add .env to
.gitignore
.gitignore
.env
node_modules
- require dotenv config in app.js
app.js
require('dotenv').config()
- Access environment variables using
process.env
app.js
mongoose.connect(
process.env.MONGO_URI,
{ useNewUrlParser: true, useUnifiedTopology: true },
() => console.log("Connected to DB")
);