Reputation: 3810
I have a simple object with constants.
{
KEY1: "value1",
KEY2: "value2"
}
I want to make them accessible globally.
I make this object globally, use it like Config.KEY1
But I am trying to find a way so that I only use KEY
to get the value.
app.js
const express = require('express')
const app = express()
//load app config variables
const config = require('config') //this returns the plain object with config keys and dvalues
...config //oops, can't do this :(
If I could do somthing like below, I think it should work.
const express = require('express')
const app = express()
//load app config variables
const KEY1 = "value1"
const KEY2 = "value2"
Upvotes: 0
Views: 53
Reputation: 371233
Destructure the config object when you import it, so that you have standalone variable names:
const { KEY1, KEY2 } = require('config');
You can also combine the config with the global object, allowing you to use those standalone variable names anywhere without importing the config everywhere, but it's not such a good idea IMO:
const util = require('./util');
Object.assign(global, util);
(make sure you don't have any properties that conflict with properties on the global object)
Upvotes: 2