Sub topic 3
Side-Effect-Only Imports
This chapter assumes you already know named and default exports, the import forms, and how to run modules in a browser or Node.js. It also assumes you are comfortable with async/await.
Side-Effect-Only Imports
A side-effect-only import runs a module but binds no names from it:
import './polyfills.js';Use this form for modules that register a polyfill, install global styles, or patch a prototype. However many times the module is imported, it runs only once.
How Modules Are Linked and Evaluated
A module goes through three phases. In parsing, the engine reads the module's import and export declarations. In linking, it connects each import to the matching export. In evaluation, it runs the module body, with dependencies running before the modules that import them. Each module specifier resolves to a single instance, so every importer of a module shares the same instance and the same state.
Top-Level await and import.meta
A module can use await outside any async function. Modules that import it wait until its promise settles. import.meta.url holds the URL of the current module, which lets you resolve files relative to the module itself.
// config.js (Node.js ES module)
import { readFile } from 'node:fs/promises';
const url = new URL('./settings.json', import.meta.url);
const text = await readFile(url, 'utf8');
export const settings = JSON.parse(text);Mixing ES Modules and CommonJS
When an ES module imports a CommonJS file, module.exports becomes the default export. If you need require itself, createRequire builds one:
// main.mjs
import { createRequire } from 'node:module';
import legacy from './legacy.cjs';
const require = createRequire(import.meta.url);
console.log(legacy === require('./legacy.cjs')); // trueCircular Dependencies
Suppose a.js is the entry point and imports b.js, and b.js imports a.js back. Then b.js evaluates first. If b.js reads a, a const that a.js has not initialized yet, it throws a ReferenceError from the temporal dead zone. There are two fixes. You can move the shared values into a third module that both files import. Or you can read the binding only inside a function that runs after evaluation finishes.
Importing JSON with Import Attributes
import config from './config.json' with { type: 'json' };
console.log(config.name);The type: 'json' attribute tells the host to parse the file as JSON and never to run it as code.
Why Static Structure Enables Tree Shaking
Import and export declarations must appear at the top level and use string literals, so bundlers can work out the dependency graph without running any code. Tree shaking is the step where a bundler removes exports that nothing imports. Dynamic import() and namespace objects used in unpredictable ways make this analysis harder.
Exercise
main.js imports './log.js' twice, once as import './log.js' and once as import { level } from './log.js'. log.js prints "loaded" at top level. How many times is "loaded" printed, and why?
Show answer
Once. Both imports resolve to the same single module instance, and a module is evaluated only once no matter how many times it is imported.
Exercise
a.js (the entry point) imports b.js, and b.js logs a const exported by a.js at top level. What happens, and what is one fix?
Show answer
b.js evaluates before a.js has initialized the const, so reading it throws a ReferenceError (temporal dead zone). One fix is to move the shared value into a third module that both files import. Another is to read it inside a function called later.