import(moduleSpecifier)
.then((module) => {
// Code to handle the loaded module
})
.catch((error) => {
// Code to handle any import errors
});
In this syntax, the moduleSpecifier
can be a string containing the path to the module that needs to be dynamically imported. The import()
function returns a Promise that resolves to the module’s namespace object, allowing you to access its exports.
Here’s an example that demonstrates the usage of dynamic imports:
// Conditionally import a module based on the user's choice
const modulePath = userChoice ? './moduleA.js' : './moduleB.js';
import(modulePath)
.then((module) => {
// Use the imported module
module.someFunction();
})
.catch((error) => {
// Handle import error
console.error('Error importing module:', error);
});
Dynamic imports are especially useful when building large applications where modules need to be loaded on-demand or conditionally based on user interactions. They enable a more efficient and optimized way of managing dependencies in JavaScript projects.
To use dynamic imports, make sure your project supports ECMAScript modules by using the type="module"
attribute in your script tags or by using a bundler like webpack that supports module loading.
#javascript #import