ES modules and named exports
ECMAScript modules (ESM) are the standard JavaScript module syntax using import and export. Node supports ESM alongside CommonJS. Use a named export when a module offers several explicitly named capabilities, and import the names you need.
Local practice: The proposed teaching runtime is Node.js 26.13.1 LTS. These original offline examples were executed on Node.js 22.16.0 (Linux x64); execution on the proposed baseline and in ByteMind is not yet verified. No npm dependencies are required. Use a fresh practice directory. Code blocks are static, not a browser-based Node runner.
Import a named calculation
export function totalCents(quantity, cents) { return quantity * cents;}import { totalCents } from "./price.mjs";console.log(totalCents(4, 225));In the directory containing the files above, run:
node main.mjsExpected standard output:
900Expected exit status: 0.
How it works
A named export makes a declaration available under its exported name. The importing module uses braces around named imports and supplies the source specifier. In native Node ESM, local relative file specifiers require explicit file extensions. .mjs selects ESM regardless of the nearby package type. An imported binding can reflect changes made by its exporting module; it is not a copied snapshot, and the importer cannot assign to the binding itself. In the first example a simple function is exported, and in the second a function updates an exported variable.
Observe a live exported binding
export let count = 0;export function increment() { count += 1; }import { count, increment } from "./counter.mjs";console.log(count);increment();console.log(count);In the directory containing the files above, run:
node main.mjsExpected standard output:
01Expected exit status: 0.
Common mistake
Do not rely on a bundler’s extensionless import behavior when running a native Node ES module. import { totalCents } from "./price" is not equivalent to the explicit .mjs specifier in this fixture. Use exact exported names or a supported alias such as import { totalCents as total } ....
Try it yourself
Add a named export discountCents(total, discount) to the pricing module and import both functions. Calculate a known order subtotal and discount. Try renaming an import using as without changing the exporter.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
ES modules and named exports — knowledge check
Check your understanding of es modules and named exports.
Which syntax imports a named export?
Why include .mjs in the relative specifier?
Why does count change after increment()?