Sub topic 11
Side-Effect-Only Imports
This chapter assumes you already know the named, default, and namespace import forms, and that you can run ES modules in a browser or in Node.js.
Most imports bring a name into scope. A side-effect-only import brings in no names. It loads a module and runs its top-level code. You write it as the keyword import followed by a specifier:
import './polyfills.js';You use this form when the module's job is to change something outside itself, for example to patch a global, register a plugin, or define a custom element.
A Worked Example: Registering Plugins
In this example, a shared registry holds plugins, and each plugin module adds itself to the registry when it runs.
// registry.js
export const plugins = new Map();// plugins/greet.js
import { plugins } from '../registry.js';
plugins.set('greet', (name) => `Hello, ${name}!`);
console.log('greet plugin registered');// main.js
import './plugins/greet.js';
import './plugins/greet.js'; // same module again: it does not run twice
import { plugins } from './registry.js';
console.log(plugins.get('greet')('Ada'));Running node main.js (with "type": "module" in package.json) prints:
greet plugin registered
Hello, Ada!This output shows two rules:
- Each module runs once. The second
import './plugins/greet.js'finds the module already loaded, so its code does not run again. - Dependencies run first. Before
main.jsruns its own body, every module it imports has been evaluated, in the order the import statements appear, and each module's own dependencies are evaluated before it.
Common Uses
| Use | Example |
|---|---|
| Polyfills | `import 'core-js/actual/array/at.js';` |
| Custom elements | `import './components/date-picker.js';` which calls `customElements.define` |
| App setup | `import './setup-env.js';` |
| Styles (bundlers only) | `import './styles.css';` |
Common Errors
Expecting a Value
import './config.js' gives you no binding. If you need something the module exports, use a named or default import instead. A side-effect import never creates a variable.
Depending on Import Order Across Files
Import declarations are hoisted, so a module's imports run before any of its other code. Order between sibling imports is predictable within one file. If two different files both rely on "setup runs first", make the dependency explicit: have each module that needs the setup import it directly.
CSS Imports Outside a Bundler
import './styles.css' works because a bundler such as Vite or webpack handles it. Browsers and Node.js reject it on their own.
Exercise
A file contains import './a.js'; import './b.js';, where a.js logs "A", b.js imports a.js and then logs "B", and the importing file then logs "main". What is printed, and why does "A" appear only once?
Show answer
The output is "A", "B", "main". a.js is evaluated first and logs "A". When b.js imports a.js, that module is already evaluated and is not run again, so b.js only logs "B". The importing file's body runs last.
Exercise
Why does import './theme.js'; console.log(theme); throw a ReferenceError even though theme.js has export const theme = 'dark';?
Show answer
A side-effect-only import creates no bindings. Use import { theme } from './theme.js'; to bring the name into scope.