Using <script type="module"> and Sharing Variables
Modern JavaScript modules (<script type="module">) have isolated scopes, meaning variables/functions declared in a module are not globally accessible by default. Here's how to share data between modules and other scripts:
1. Export/Import (Module-to-Module)
Use export in one module and import in another.
Example:
<!-- Module 1 (exports) -->
<script type="module">
export const myVariable = "Hello"; // Export variable
export function myFunction() { // Export function
return "World!";
}
</script>
<!-- Module 2 (imports) -->
<script type="module">
import { myVariable, myFunction } from './path/to/module1.js';
console.log(myVariable); // "Hello"
console.log(myFunction()); // "World!"
</script>
Note: Inline modules (in HTML) cannot directly import from other inline modules. Use external files.
2. Expose to Global Scope (Module → Classic Script)
Explicitly attach variables to window to make them global.
Example:
<!-- Module (exposes globally) -->
<script type="module">
const moduleSecret = "I'm private"; // Not accessible outside
window.myGlobal = "I'm global!"; // Attach to window
</script>
<!-- Classic Script (accesses global) -->
<script>
console.log(window.myGlobal); // "I'm global!"
console.log(moduleSecret); // Error! (not defined)
</script>
3. Events/Custom Events (Async Communication)
Use events to communicate between modules and classic scripts.
Example:
<!-- Module (dispatches event) -->
<script type="module">
const data = { user: "Alice" };
// Dispatch custom event when ready
document.dispatchEvent(new CustomEvent("moduleReady", { detail: data }));
</script>
<!-- Classic Script (listens for event) -->
<script>
document.addEventListener("moduleReady", (e) => {
console.log(e.detail.user); // "Alice"
});
</script>
4. Shared Global Object
Create a global namespace to avoid polluting window.
Example:
<!-- Module (sets global namespace) -->
<script type="module">
window.MyApp = window.MyApp || {};
window.MyApp.sharedData = { count: 42 };
</script>
<!-- Classic Script (accesses namespace) -->
<script>
console.log(MyApp.sharedData.count); // 42
</script>
Key Considerations:
-
Module Scope:
- Variables in
type="module" are scoped to that module.
- Use
export/import for inter-module communication.
-
Execution Order:
- Classic scripts run immediately (unless
defer/async).
- Modules are deferred by default (run after HTML parsing).
- Use
DOMContentLoaded or events if classic scripts depend on modules.
-
Security:
- Modules use strict mode by default (e.g.,
this is undefined).
- Avoid global variables when possible to prevent conflicts.
When to Use What:
| Scenario | Solution |
|---|
| Module → Module | export / import |
| Module → Classic Script | Attach to window or events |
| Classic Script → Module | Events or global callbacks |
| Avoid global pollution | Custom events or shared objects |
By combining these techniques, you can safely share data between modules and classic scripts while maintaining clean code structure.