Course contentsShow
← JavaScript
Lesson 1408 of 2,72331. ModulesFree lesson

Type='module' in HTML

Learn how to load ES modules in the browser using script tags with type='module'.

Type='module' in HTML

You'll learn: How to load ES modules in the browser using the <script type="module"> tag.

The Browser's Module Loader

Now that you understand how to write ES modules with import and export, you need to tell the browser to treat your JavaScript file as a module. By default, regular <script> tags load JavaScript in "classic script" mode, where import and export don't work. To enable module features, you add type="module" to your script tag.

Basic Syntax

<!-- Classic script (no modules) -->
<script src="app.js"></script>

<!-- ES Module -->
<script type="module" src="app.js"></script>

When you use type="module", the browser:

  • Allows import and export statements
  • Automatically runs code in strict mode
  • Defers execution until the document is parsed (like defer attribute)
  • Treats each module with its own scope (no global pollution)

Inline Module Scripts

You can also write module code directly in HTML:

<script type="module">
  import { greet } from './utils.js';
  greet('World');
</script>

Important Differences

Modules behave differently from classic scripts:

  • CORS applies: Module files must be served from the same origin (or with proper CORS headers)
  • Always deferred: No need for defer attribute
  • Unique execution: Each module runs once, even if imported multiple times

Key Takeaway: Use <script type="module"> in your HTML to enable ES module syntax—this tells the browser to process imports, exports, and enforce module scope rules automatically.