Skip to main content

Named vs default export

1. What "export" means in general

Export is the mechanism that lets a module "hand out" functions, variables, classes, and so on, so they can be imported in other files.

ES Modules have two types of exports:

  1. Named export
  2. Default export

2. Named exports

Lets you export several values from a module, each with its own name.

math.js

javascript
export const PI = 3.14; export function add(a, b) { return a + b; } export function sub(a, b) { return a - b; }

app.js

javascript
import { PI, add, sub } from './math.js'; console.log(PI); // 3.14 console.log(add(2, 3)); // 5

You can export several items from one file. The import must use the exact same names as the export.

An alternative form:

javascript
const PI = 3.14; function add(a, b) { return a + b; } function sub(a, b) { return a - b; } export { PI, add, sub };

This is equivalent to the previous example, just a grouped export.

3. Default exports

Lets you export only one value as the module's "main content".

logger.js

javascript
export default function log(message) { console.log('LOG:', message); }

app.js

javascript
import log from './logger.js'; // no braces! log('Hello'); // LOG: Hello

With a default export:

  • a module can have only one default export;
  • you can name it anything you like on import.

Another example:

javascript
export default class User { constructor(name) { this.name = name; } }

Import:

javascript
import Person from './User.js'; // any name works const user = new Person('Tim');

4. The key differences

CriterionNamed exportDefault export
CountCan export many valuesOnly one value
ImportWith braces {}Without braces
Name on importMust match the originalAny name works
Syntaxexport const foo = ... / export { foo }export default ...
Importing everythingimport * as lib from './file.js'Not directly applicable
SemanticsExports "many different parts" of a moduleExports the module's "main entity"
ExamplesUtilities, constants, functionsThe main class, function, or component

5. They can be combined

One module can hold both a named and a default export:

math.js

javascript
export const PI = 3.14; export function add(a, b) { return a + b; } export default function multiply(a, b) { return a * b; }

app.js

javascript
import multiply, { PI, add } from './math.js'; console.log(multiply(2, 3)); // 6 console.log(PI); // 3.14 console.log(add(5, 5)); // 10

6. Importing "everything" from a module

You can import the whole content under one name:

javascript
import * as math from './math.js'; console.log(math.PI); // 3.14 console.log(math.add(2, 3)); // 5 console.log(math.default(2, 3)); // 6, the default is reached via .default

7. Common mistakes

MistakeCauseFix
SyntaxError: The requested module ... does not provide an export namedThe wrong name when importing a named exportUse the exact names
default is not exportedTrying to import a default that doesn't existCheck that you export it via export default
Cannot use import statement outside a moduleThe file isn't ESMAdd "type": "module" to package.json or use .mjs

8. When to use which

ScenarioExport type
A module with several utilitiesNamed
One main class / componentDefault
A library with an APICombined (default + named)

Examples:

  • Reactexport default React; export { useState, useEffect }
  • Axiosexport default axios; export { AxiosError }

9. Conceptually

  • Named exports are "many tools from a set" (everything is explicit, the IDE helps, autocomplete works).
  • A default export is "the module's main entity" (convenient when a file is responsible for one central object).

Quick summary

CharacteristicNamed exportDefault export
CountSeveralOne
Importimport { x } fromimport x from
NameMust matchAny name
IDE hintsWorks greatSometimes lost
Use caseUtilities, functions, constantsThe main class or component
Combining themWorks togetherWorks together

In one sentence

Named exports let you export several entities under their own names, while a default export is for the module's single "main" entity, which can be imported under any name.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.