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

158
Views
why I can't get all folder's module with require('./folder')

i am using this:

index.js
functions
└abc.js
└reset.js
functions.js

functions.js

const fs = require("fs")

const functions_ = {}
const files = fs.readdirSync("./functions").filter(file => file.endsWith('.js'));

for (file of files) {
  var file_name = file
  file_name = file_name.substr(0,file_name.search("\\."))
  functions_[file_name] = require(`./functions/${file}`)
};

module.exports = functions_

In short, I create a function with that folder name for each folder. i tried this code to make it much easier

require('./functions');

but it didn't work

Isn't there a way to make this much easier? (The title may be wrong, I couldn't find what title to put.)

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The short answer is no.

For the long answer: You can't because require/import statements in all languages are configured to import files and not folders. So you can't require a folder because the folder technically doesn't have valid readable/executable code. But when you require/import a file in JavaScript, the exported parts of that file can be used in the "importee" file (the file from which other files are being imported).

No matter the number of files, you need to statically import them as you need them.

Proposed Solution

You can import everything in that folder into an index.js file in that very folder. Then import everything from ./folder/index, that can work too.

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