How to use the loop to import multiple js files and export all those js files

Viewed 605

I've revised the question to help people understand what I'm looking for help with. I have a directory tree as follows:

--action
  --a.js
  --b.js
  --c.js
  --d.js
--data
  --list.js
--export
  --exp.js

Files js: a, b, c, d all have 1 function as follows:

module.export = {
  "test": function name () {
    //do something
  }
}

In the list.js file. I have an array like this.

const list = [ 'a', 'b' ];

At the exp.js file, I want to export all js files in the action directory through an array in the list.js

Desired: use the loop to import multiple js files and export all those js files.

Thank you everyone for reading and helping.

1 Answers

V8 provides you with an option called Dynamic Import which helps you to import file dynamically. dynamic import loads your module and returns a module object that contains all its exports. In order to access the default export use the default property of the module object.

const moduleA = await import ('./moduleA');
moduleA.default();

// OR

const { default: moduleA } = await import ('./moduleA');
moduleA();

Related