While I can understand the benefit of using a .env
file, what are the pros and cons (if any) of using them versus placing the values directly in the code? I've see many guides that explain how to use them, but never why should we use them. Are they considered a best practice? Should every production project use them?
# File: .env
DB_HOST=localhost
DB_USER=rootz
DB_PASS=s1mpl3
# File: random_db.js
const db = require('db')
db.connect({
host: process.env.DB_HOST,
username: process.env.DB_USER,
password: process.env.DB_PASS
})
It is best practice not to include database configuration information directly in the code. Keeping these items in environment variables instead has the following benefits:
I'm sure there are other reasons that I am not thinking of right now, but these alone are enough to convince most people.