Skip to content

Instantly share code, notes, and snippets.

@thoughtspeed7
Last active November 14, 2022 09:15
Show Gist options
  • Save thoughtspeed7/ad53e9be2932053bbe967f39ca490895 to your computer and use it in GitHub Desktop.
Save thoughtspeed7/ad53e9be2932053bbe967f39ca490895 to your computer and use it in GitHub Desktop.
Node.js Best Practices - Smarter Ways to Manage Config Files and Variables
// 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