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

138
Visualizações
Is it possible to write 'non-independent' modules in Javascript?

I have an Express-server with routes and functions, that i would like to segment into seperate files. I understand this question has been posted hundreds of times, but after browsing through 20+ threds and guides I still can't manage to find a good answer.

I have a situation that looks like this.

function securityCheck(){
   do stuff...
}

function anotherFunction(){
   do stuff...
}

app.get("/login", (req,res)=>{
   if (securityCheck()){
   do stuff...
})

app.get("/backend", (req,res)=>{
   if (anotherFunction()){
   do stuff...
})...

and I would like to have it like this

    embed(./securityCheck.js)
    embed(./anotherFunction.js)
    embed(./login.js)
    embed(./backend.js)
    ...

I understand that module.exports can pretty much do this.

However, since it's evaluating code from the relative directory of the module it self, it causes issuse with dependencies. If i use for example the securityCheck() function I have to require that locally in the module, and it also makes me reorganize my paths.

I don't have any need for the modules functionality.

Im not looking for smart ways to reuse functions or make the project modular. I just want do segment my code for the purpose of cleanliness.

The thing im looking for is something like Eval, that just runs code in it's current context that would basicly import a textfile with the code in it.

Is this possible with Javascript and Node?

about 4 years ago · Juan Pablo Isaza
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