Course contentsShow
← JavaScript
Lesson 1452 of 2,72331. ModulesFree lesson

The Default Export Wrapper

Why CommonJS modules appear as a default export in ESM and how to destructure named exports.

The Default Export Wrapper

You'll learn why CommonJS modules look like default exports when imported into ESM, and how to work with their named exports.

Why CommonJS Becomes a Default Export

When you import a CommonJS module into ESM, Node.js (and bundlers) automatically wrap the entire module.exports object as the default export. This happens because CommonJS doesn't have the concept of "named" vs "default" exports—it just exports one thing: whatever you assign to module.exports.

Think of it like receiving a gift box (the default export) that contains all the items inside (the named exports). You must open the box first before accessing individual items.

The Import Pattern

// A CommonJS module exports an object
// utils.cjs
module.exports = {
  add: (a, b) => a + b,
  subtract: (a, b) => a - b
};

// When importing in ESM:
import utils from './utils.cjs';  // Gets the whole object
console.log(utils.add(2, 3));     // 5

Destructuring Named Exports

You cannot directly use named import syntax with CommonJS modules:

// This WON'T work:
// import { add } from './utils.cjs';  // Error!

Instead, you have two options:

Option 1: Import default, then destructure

import utils from './utils.cjs';
const { add, subtract } = utils;

Option 2: Destructure in the import statement

// This works, but still imports the default:
import { default as utils } from './utils.cjs';
const { add } = utils;

Key Takeaway: CommonJS modules imported into ESM always appear as a single default export containing the module.exports object. You must destructure or access properties to get individual named values—ESM named imports don't work directly with CommonJS.