Categories
JavaScript Answers

How to conditionally import an ES6 module with JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *