Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

147
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda