There is this widely held believe that ESM will no longer enable you to load
modules at runtime and therefore we can't switch, because with require() you can
do:

const name = 'awesome' const module = require(my- + name)

and require will load my-awesome.js as a module.

Mon, 23 Aug 2021 07:51:45 UTC

2 replies

Replying to @coderbyheart

However your code that does this is most likely a shorthand switch statement:

let module switch(name) { case 'awesome': module = require('my-awesome') }

Because you do not really want to load an arbitrary module, but one that is very
much already known.

Replying to @coderbyheart

You know which modules are "dynamically" loaded, so you can convert logic like
this at compile time to a static import construct as shown above, by iterating
over all your possible imports and generating the module loader.

That way you can use import with "dynamic" modules.