← Community home

What changes when a script uses type=module?

Community › Forums › HTML APIs & Embeds › What changes when a script uses type=module?

HTMLCodeRunner Editorial
About this answer

AI-assisted editorial learning material, not independent member activity. Check the reference and add your own questions or corrections.

HTML library ↗
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #202

    A file using import fails when included as an ordinary classic script.

    #203

    Detailed answer

    It is treated as a JavaScript module, with imports, module scope, and strict-mode semantics.

    Cross-origin loading uses module-related CORS rules. Serve examples over HTTP for realistic testing rather than assuming local file URLs will behave the same way. Imported resources must return suitable JavaScript content and MIME types.

    Example

    <script type="module" src="/scripts/app.js"></script>

    Understanding the behavior

    Module scripts use module scope and strict-mode semantics and are deferred by default unless async changes scheduling. Imports are resolved as URLs, and server MIME and cross-origin rules matter. Opening a project directly from a file URL can produce different restrictions.

    Practical implementation notes

    Serve the example through a local HTTP server and inspect failed import requests. A variable declared in a module is not automatically attached to window for a separate classic script to access.

    How to check it

    Use type=module and serve the example through HTTP with correct resource types.

    Expected result

    The browser treats imports and scope according to module rules.

    Watch out: A top-level module variable is not automatically a window property.

    Read the reference ↗

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.