JavaScript Module Scripts Are Deferred by Default

JavaScript module scripts are deferred by default when written in the initial HTML without async. The browser can load the entry point and its dependencies while parsing, then begin evaluation after the document has been parsed.

That makes a module in the head a reasonable place for code that enhances existing markup. Adding defer is unnecessary, but “deferred” should not be mistaken for “every asynchronous task has finished”.

Module scripts are deferred, so head placement works

A small page can declare its entry point alongside its other resources:

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Module timing example</title>
    <script type="module" src="./main.js"></script>
  </head>
  <body>
    <button id="greet" type="button">Say hello</button>
    <p id="message"></p>
  </body>
</html>

Save the entry point as main.js beside the HTML file:

import { setupGreeting } from "./greeting.js";

setupGreeting();

The imported behaviour lives in greeting.js:

export function setupGreeting() {
  const button = document.querySelector("#greet");
  const message = document.querySelector("#message");
  if (!button || !message) return;

  button.addEventListener("click", () => {
    message.textContent = "Hello from the module.";
  });
}

The button and paragraph from the initial document exist before main.js begins evaluation. The function can attach its listener without wrapping this startup call in DOMContentLoaded.

The HTML script definition specifies this normal non-async module path. A dynamically inserted script or an explicitly async entry point needs its own timing assumptions.

An import is a dependency, not another ordered tag

The entry point names the function it needs through a static import. The browser loads and links the module graph, and a dependency is evaluated before code that needs its completed evaluation.

MDN’s module guide explains relative import paths and module scope. In this example, ./greeting.js resolves relative to main.js, rather than whichever HTML page happened to load it.

That explicit relationship is useful when the script grows. A helper can move into a separate file without asking every template author to remember a new sequence of classic script tags.

Module scope also changes how code is exposed. A top-level declaration stays in its module unless deliberately exported or attached to a global object; it does not automatically become window.setupGreeting.

Inline modules follow the same default

An inline module can query markup that appears later in the document:

<script type="module">
  const status = document.querySelector("#status");
  status.textContent = "The initial HTML has been parsed.";
</script>

<p id="status">Waiting for the module.</p>

An ordinary inline classic script at that location would execute before the paragraph is parsed. Putting defer on the classic inline script would not make it follow module timing.

The script attribute reference documents that distinction. The module’s type supplies the deferred behaviour, including when there is no external source file.

Async opts out of waiting for parsing

Adding async changes when the module can run:

<script type="module" src="./independent.js" async></script>

Its entry point can run when the module graph is ready, without waiting for the document to finish parsing. The graph’s dependencies still matter, but the rest of the HTML might not yet exist.

Use that form only when the module can tolerate those conditions. For the greeting button, the default timing expresses the requirement more directly than adding a second readiness mechanism around an async script.

Await can outlive DOMContentLoaded

After a module suspends at an await, other browser work can continue. Top-level await also affects modules that depend on the awaited result.

The DOMContentLoaded documentation explicitly distinguishes parsing from asynchronous continuations. Do not treat that event as proof that a module’s fetched configuration or other awaited work has completed.

Wait for the task’s promise when another part of the page needs its result. DOMContentLoaded does not tell you whether that task finished.

Serve the example as a website

Browser modules need suitable JavaScript MIME types, and cross-origin imports are subject to CORS. Opening the HTML directly through file:// is also a poor way to test module loading; use a local HTTP server.

The module deployment guidance covers these constraints. A valid import can still fail because the server returns an HTML error page for a missing JavaScript file.

No bundler is required for these three files. For a small page enhancement, a head entry point and explicit imports make the dependency clear while leaving the HTML available first. The broader script-loading comparison helps when classic and independent scripts share the page too.