Simplify exporting all files in a folder to import them using 'destructered import'

41 Views Asked by At

I have a folder extractors with a bunch of files each containing a function. (Yes, I have my reasons why I've seperated it). I want to export all of the files in a way that allows me to import them (destructered):

const { ..., ..., ...} = require(...);

First things first, my current code works. However, I'm trying to find a better solution since the way I'm doing it now feels a bit 'janky'.

Currently, I have an index.js file in the extractors folder which exports the files:

const AccountData = require('./Account');
const BitmojiData = require('./Bitmoji');
//...

module.exports = {
    AccountData,
    BitmojiData,
    //...
}

This allows me to use:

const { AccountData, BitmojiData, ... } = require('./extractors/index.js');

And prevents me from having to do:

const AccountData = require('./extractors/AccountData.js');
const BitmojiData = require('./extractors/BitmojiData.js');
//...

However, I can't help but feel like what I'm doing is.. 'suboptimal'. So I'm trying to find a better solution, but couldn't find one.

I hope someone is able to help me out, thanks in advance!

Example function /extractors/Example.js

const Example = (data) => {
   //...
   return ({
      k: 'v'
   })
}

module.exports = Example;

Also, I'm pretty sure I'm on ES5, I can't use export default etc.

0

There are 0 best solutions below