Last active
November 14, 2022 09:15
-
-
Save thoughtspeed7/ad53e9be2932053bbe967f39ca490895 to your computer and use it in GitHub Desktop.
Node.js Best Practices - Smarter Ways to Manage Config Files and Variables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// om namah shivay | |
// requires | |
const _ = require('lodash'); | |
// module variables | |
const config = require('./config.json'); | |
const defaultConfig = config.development; | |
const environment = process.env.NODE_ENV || 'development'; | |
const environmentConfig = config[environment]; | |
const finalConfig = _.merge(defaultConfig, environmentConfig); | |
// as a best practice | |
// all global variables should be referenced via global. syntax | |
// and their names should always begin with g | |
global.gConfig = finalConfig; | |
// log global.gConfig | |
console.log(`global.gConfig: ${JSON.stringify(global.gConfig, undefined, global.gConfig.json_indentation)}`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment