Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

146
Views
How to DRY requires in a Node Express app?

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?

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!