Default exports and package type
An ES module can expose one default export and any number of named exports. A default import chooses a local name without braces. Node uses file extensions and package configuration to decide whether source is ESM or CommonJS.
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 default function
export default function format(cents) { return `${cents} cents`;}import displayPrice from "./format.mjs";console.log(displayPrice(250));In the directory containing the files above, run:
node main.mjsExpected standard output:
250 centsExpected exit status: 0.
How it works
export default marks a module’s default export. The consumer may choose its own name for that value. A package.json file with "type": "module" makes ordinary .js files in its package scope ES modules; "type": "commonjs" selects CommonJS for .js. .mjs and .cjs explicitly override the package mode for their files. New Node versions may detect syntax in ambiguous .js files, but an explicit package type or extension avoids relying on version-dependent detection. A package.json is JSON metadata, not executable JavaScript.
Use an explicit package module type
In the directory containing the files above, run:
node main.jsExpected standard output:
CatalogExpected exit status: 0.
Common mistake
An import without braces requests the default export, not any named export with the same spelling. A default export may itself be an object; that does not make its properties individual ESM named exports. Explicitly document package mode, especially when mixing .cjs and .js files.
Try it yourself
Add a named export beside the default formatter. Import the default under a different local name and the named export in braces. In a separate practice folder with "type": "module", try a .cjs file using module.exports and explain why it remains CommonJS.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Default exports and package type — knowledge check
Check your understanding of default exports and package type.
Can a default import use a different local name from the exporting function?
What format does main.js use in the shown package?
What format does a .cjs file use inside type module?