Categories
JavaScript Answers

How to import module from URL with JavaScript?

Spread the love

Sometimes, we want to import module from URL with JavaScript.

In this article, we’ll look at how to import module from URL with JavaScript.

How to import module from URL with JavaScript?

To import module from URL with JavaScript, we use import with a URL.

For instance, we write

<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8" />
    <title>Hello World</title>
    <script type="module" src="./hello.js"></script>
  </head>
  <body></body>
</html>

to load the hello.js module with a script tag.

Then we write

import ip6 from "https://cdn.jsdelivr.net/gh/elgs/ip6/ip6.js";

to import the module located at https://cdn.jsdelivr.net/gh/elgs/ip6/ip6.js in hello.js.

Conclusion

To import module from URL with JavaScript, we use import with a URL.

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 *