JavaScript Export Default: Syntax and Examples

Importing values from JavaScript modules means matching the import to the way each value is exported. A default export marks one value as a module’s default, and the importer can bind it to a chosen local name without braces.

This article explains what a default export represents, then shows how to export and import one alongside named exports.

TL;DR

JavaScript’s export default marks one value as a module’s default export, which the importer receives without braces and may bind to a local name of its choice.

  • Use a default export for the module’s primary value.
  • Use braces for named exports, not for the default binding.
  • A module can have one default export and any number of named exports.

What Is export default in JavaScript?

A default export is the single value a JavaScript ES module exposes as its default. An ES module is a JavaScript file with its own scope and explicit import and export declarations.

A default export may be a function, class, object, or expression. The export declaration can appear beside the declaration or reference a value declared earlier.

At the import site, write import greet from ‘./greet.js’ without braces. The importer chooses its local binding name.

A named export is requested through its exported name. MDN’s export reference documents both declaration forms.

Default exports belong to ECMAScript modules. Node.js CommonJS uses module.exports, a separate module format. Follow the format used by the project.

How to Export and Import a Default Value

Place the exported value in one module, import it from a second module, then run the importer with Node.js. I used Node.js v26.7.0 and `.mjs` files so Node treats these files as ES modules without package configuration.

Step 1: Export one value

Save this source as export_default_demo.mjs.

export default function greet(name) {
  return `Hello, ${name}`;
}

Step 2: Import and run it

Save the following as import_demo.mjs. It imports the same default twice under two local names and compares the bindings, which checks whether those names refer to the same function.

import greet from './export_default_demo.mjs';
import renamed from './export_default_demo.mjs';
console.log(greet('Ada'));
console.log(renamed === greet);

Run node import_demo.mjs. I got this output:

Hello, Ada
true
Terminal showing a default import greeting and matching binding

The output confirms the function runs and both local bindings refer to the same exported function. Both names point to the same function.

Step 3: Combine default and named exports

Use a named export for supporting values while keeping one default for the primary value. The importer can request both in a single declaration:

// greetings.mjs
export const language = 'en';
export default function greet(name) {
  return `Hello, ${name}`;
}

// app.mjs
import greet, { language } from './greetings.mjs';

The default binding comes first without braces. The named binding is grouped in braces. The MDN import reference describes this form.

Common Mistakes and Fixes

Check the source declaration when an import fails. The source declaration determines whether braces belong in the import.

SymptomCauseFix
Named export `greet` is missingBraces request a named export from a default-only moduleUse `import greet from ‘./export_default_demo.mjs’`
Duplicate default declarationThe module declares `export default` twiceKeep one default and make other values named exports
Module cannot be foundImport path or file extension does not matchCheck the relative path and file name

I deliberately imported greet with braces from a module that exports only a default. Node.js returned:

SyntaxError: The requested module './export_default_demo.mjs' does not provide an export named 'greet'
Terminal showing the named import error for a default-only module

I remove the braces when the module exports a default. A named greet export is the alternative when callers need that binding. I choose a default for this example’s primary function because the importer needs no exported name.

Node.js reported SyntaxError: Identifier ‘.default’ has already been declared when I ran a module with two default declarations. Supporting values can use named exports. The Node.js modules guide covers the broader module context, while the Node.js functions guide explains the function used in this example.

Terminal showing duplicate default export syntax error

Conclusion

Check the module declaration when an import fails. A requested name must exist in the form the module exports.

For the formal rules, consult the MDN JavaScript modules guide. For a broader comparison of import and export forms, see JavaScript.info’s import and export chapter.

FAQ

These answers clarify what the default export represents and when the syntax applies.

Does export default export an object?

No. It exports the value that follows the declaration. That value may be an object, function, class, or another expression.

Does export default export everything in a file?

No. It exports one default value. Other values must be exported separately, usually with named exports.

When should I use a default export?

Use one when the module has one clear primary value. Use named exports when callers should choose among several peer values.

Vaibhav Raj
Vaibhav Raj

Vaibhav Raj writes about Node.js development and maintenance, including security, dependencies, modules, debugging, AWS integration, and running services. His work also covers database choices and Python tools.

Articles: 11