ES6 import all modules from a directory

44 Views Asked by At

We have a ES5 project where we used the import-modules library to import all files of a directory and execute there run()-function that I am now migrating to ES6.

...
import importModules from 'import-modules';
const initHooks = importModules('../../hooks/onInitStart');

export const executeInitHooks = async () => {
  logger.info(`Executing Init hooks`);
  for (const [name, hook] of Object.entries(initHooks)) {
    try {
      logger.info(`Import ${name}`);
      await hook.run();
    } catch (err) {
      logger.error(`Hook ${name} couldn't be executed`);
      logger.error(err);
    }
  }
};

As I didn't come up with the idea of using the import-modules library , is that good-pratice or could it be done in a better way using the possibilities of ES6?

PS: I already read the post import modules from all files in a directory .

0

There are 0 best solutions below