Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Module scripts: script type=module

Setting `type="module"` marks the script as a module script. Inline code is treated as a JavaScript module. The HTML Standard also allows `src` to reference a WebAssembly entry module, but browser support varies. JavaScript modules support `import` and `export`, run in strict mode automatically, and have their own scope for declarations.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

<script type="module">/* JavaScript module code */</script>
<script type="module" src="app.js"></script>

The symbol script type=module denotes a script element with type="module". Place code inside the element or reference a file through src. This is HTML's integration with JavaScript modules, not a separate ECMAScript edition.

Modules support import and export and run in strict mode. Top-level declarations, including var, do not create properties on window. In the example, moduleValue remains in module scope.

A module written in the document markup, without async, runs after HTML parsing finishes and its dependencies are ready. This lets the example access the later result element. The defer attribute has no effect on modules; async allows a ready module to run without waiting for parsing to finish.

The required MIME type depends on the requested module type. A JavaScript entry module fetched via src requires a JavaScript MIME type. The HTML Standard also allows src to reference a WebAssembly entry module served as application/wasm, but browser support for this varies. A JSON dependency imported with with { type: "json" } requires a JSON MIME type. Cross-origin fetching requires CORS permission. Check support in your target browsers; Node 24 in the repository does not establish browser support.

Examples

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Module script</title>
  <script type="module">
    var moduleValue = 42;
    document.getElementById("result").textContent =
      `${moduleValue}; ${Object.hasOwn(window, "moduleValue")}`;
  </script>
</head>
<body>
  <output id="result"></output>
</body>
</html>

Result

42; false

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources