Reputation: 8982
I have a Node + Express app. In many of my files I am doing this at the top
const config = require('./config');
const Twit = require('twit');
const TwitConnector = new Twit(config);
Is there a way to DRY this, so I don't have to repeat this everywhere?
Is there a, best practice, pattern to make something like TwitConnector
globally available so that I can use it anytime I need it?
Or maybe that's not a good idea and explicitly requiring it is the right thing to do?
Upvotes: 1
Views: 75
Reputation: 1339
Can't you make twit-connector.js file and require that instead? I don't think making it global is a good idea.
twit-connector.js
const config = require('./config');
const Twit = require('twit');
const TwitConnector = new Twit(config);
module.exports = TwitConnector;
somefile.js
const TwitConnector = require('./twit-connector');
// do something with TwitConnector
Upvotes: 2