Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

148
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda