Skip to content

The export name "__esModule" is reserved and cannot be used #1591

Description

@heypiotr

Recently, I've been attempting to bundle modules from jspm.io (via a custom esbuild plugin which implements imports from URLs and import maps), and I've been running into this error a lot:

error: The export name "__esModule" is reserved and cannot be used (it's needed as an export marker when converting ES module syntax to CommonJS)

Here's an example module where I've run into this for the first time: https://jspm.dev/npm:react-calendly@2.2.1!cjs

It all boils down to something like this:

// MRE.js

const exports = {}
Object.defineProperty(exports, "__esModule", { value: true })
export default exports

const __esModule = exports.__esModule
export { __esModule }

It's that named __esModule export that upsets esbuild -- if I comment it out, it builds fine.

The way I understood it from reading esnext/es6-module-transpiler#85, the magic __esModule export was introduced so that when you transpile an ESM module to CJS, and do const foo = require("esm-foo-transpiled-to-cjs"), it'll know that you really mean const foo = require("esm-foo-transpiled-to-cjs").default. However, I belive JSPM's doing things the other way, i.e., it transpiles CJS modules to ESM. So I'm not sure if their usage of the __esModule export is correct.

We've had a brief conversation about this with @guybedford on the JSPM Discord, but we decided to move it here. I'm curious, what's the story behind this "__esModule is reserved" check/error in esbuild?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions