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

137
Views
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
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!