To use variables for the module path, you need to construct the path as a string concatenation or template literal and then pass it to the import()
function.
Here’s an example:
const moduleName = './myModule.js';
import(moduleName)
.then((module) => {
// Use the imported module
module.myFunction();
})
.catch((error) => {
console.error('Dynamic import failed:', error);
});
In the above example, the moduleName
variable holds the path to the module you want to import. The import()
function dynamically imports the module at runtime and returns a promise.
You can use the imported module as you would with a regular static import.
It’s worth mentioning that the variable used in the module path can be any type, as long as it evaluates to a string representing a valid module path.
Dynamic imports are helpful when you want to conditionally load modules or load them on-demand. They provide flexibility and improve code performance by allowing the selective loading of modules.