Skip to content

Default import from an ESM package into a CommonJS project with esModuleInterop is faulty #58341

@JonasDoe

Description

@JonasDoe

🔎 Search Terms

"cjs esm esModuleInterop default", "common esm esModuleInterop default"

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about default imports from ESM to CommonJS
  • I'm using TypeScript 5.4.5

⏯ Playground Link

https://github.com/JonasDoe/typescript-esm-issue

💻 Code

With esModuleInterop=true:

import { default as Provider } from 'oidc-provider'; // this is an ESM project with export default class Provider
console.log('Created new Provider:', new Provider('http://example.tld'))

The transpiled code will be:

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const oidc_provider_1 = __importDefault(require("oidc-provider"));
console.log('Created new Provider:', new oidc_provider_1.default('http://example.tld'));

🙁 Actual behavior

  • Create a CommonJS project with Node22, so --experimental-require-module is supported.
  • Import the default export of an ESM dependency
  • Enable esModuleInterop
  • Transpilation will create an object like {"default": {"default": the-imported-object}}, so the-imported-object isn't found

🙂 Expected behavior

  • Transpilation should create an object like {"default": the-imported-object}, so without the double nesting.

Additional information about the issue

The original suggestion that this is a bug in TypeScript came from here: panva/node-oidc-provider#1249 (reply in thread), and by a person with greater knowledge about Node and TypeScript. So I'm sorry if I left something unclear.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ExternalRelates to another program, environment, or user action which we cannot control.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions