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

104
Views
Can I dynamically import an ES6 module?

I'm trying to have subcommands in my program, but I want to have them in a list and import them all at once. Can I do something like this:

const subcommands = ['help', 'add', 'remove'];
for (const subcommand of subcommands) {
  import {main} from './'+subcommand+'.js';
  main();
}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Yes there is an function for dynamic imports. mdnimport().
It returns a promise so you can put a then after it, or use async await.

(async () => {
  const subcommands = ['help', 'add', 'remove'];
  for (const subcommand of subcommands) {
    const {main} = await import('./'+subcommand+'.js');
    main();
  }
})()
about 4 years ago · Juan Pablo Isaza Report

0

You can use the dynamic-import function.

const subcommands = ['help', 'add', 'remove'];
for (const subcommand of subcommands) {
  import('./'+subcommand+'.js').then(i => {
     i.main()
  })
}
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!