dmnd/dedent

Node ESM compatibility issue

otakustay opened this issue · 2 comments

When using dedent in a node ESM environment, a import dedent from 'dedent' statement will result a {default: dedent} object.

> const dedent = await import('dedent')
undefined
> dedent`abc`
Uncaught TypeError: dedent is not a function
> dedent('abc')
Uncaught TypeError: dedent is not a function
> dedent
[Module: null prototype] {
  __esModule: undefined,
  default: <ref *1> [Function: dedent] { default: [Circular *1] }
}
> dedent.default`abc`
'abc'

This issue comes from several reasones:

  1. In package.json a module filed is specified to dist/dedent.mjs, but NodeJS doesn't support this field
  2. The main field in package.json references to dist/dedent.js which is a CommonJS module, NodeJS's ESM then transforms module.exports into a default export

To address this, we need a exports field in package.json:

{
  "exports": {
    ".": {
      "types": "./index.d.ts",
      "import": "./dist/dedent.mjs",
      "default": "./dist/dedent.js"
    }
  }
}

This will solve the import issue, but is potentially a breaking change to NodeJS ESM environment, would it worse a major version?

Ah thanks for filing @otakustay! I think we can treat this as a bug and release as 1.2.0 / minor version change. dedent.default is a circular reference, so folks who were using it like dedent.default(...) should generally not be broken.

Released as dedent@1.3.0 🚀