Sometimes, we want to conditionally import an ES6 module with JavaScript.
In this article, we’ll look at how to conditionally import an ES6 module with JavaScript.
How to conditionally import an ES6 module with JavaScript?
To conditionally import an ES6 module with JavaScript, we can use the import
function.
For instance, we write
const myModule = await import(moduleName);
in an async function to call import
with the moduleName
string to import the module named moduleName
.
And then we get the module returned by the promise returned by import
with await
.
Conclusion
To conditionally import an ES6 module with JavaScript, we can use the import
function.