Is it possible to have a single .env file for all different deployment environments such as development, production , etc.Based on the environment the corresponding environment variables file needs to be loaded.
Yes. You can use the dotenv module
for example:
.env
DB_HOST=localhost
DB_USER=root
DB_PASS=s1mpl3
app.js
require('dotenv').config()
const db = require('db')
db.connect({
host: process.env.DB_HOST,
username: process.env.DB_USER,
password: process.env.DB_PASS
}
Yes, not necessarily .env file but a json/js file.
You can make a file like below and require this file with environment -
let config = require('./pathToFile/')[process.env.NODE_ENV]
Your file -
{
"development" : {
"dbConfig" : {
"username" : "acaca",
"password" : "ajbcjdca",
"port" : "acdc",
"etc" : "etc"
},
"serverConfig" : {
"host" : "jabcjac.com",
"port" : "4545",
"etc" : "etc"
},
"AWSConfig" : {
"accessKey" : "akcakcbk",
"etc" : "etc"
}
},
"production" : {
"dbConfig" : {
"username" : "acaca",
"password" : "ajbcjdca",
"port" : "acdc",
"etc" : "etc"
},
"serverConfig" : {
"host" : "jabcjac.com",
"port" : "4545",
"etc" : "etc"
},
"AWSConfig" : {
"accessKey" : "akcakcbk",
"etc" : "etc"
}
},
"test" : {
"dbConfig" : {
"username" : "acaca",
"password" : "ajbcjdca",
"port" : "acdc",
"etc" : "etc"
},
"serverConfig" : {
"host" : "jabcjac.com",
"port" : "4545",
"etc" : "etc"
},
"AWSConfig" : {
"accessKey" : "akcakcbk",
"etc" : "etc"
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With